Reputation: 23
In my app an alarm creates a notification which works but I am trying to pass a string value from my main class FoodItems.java to AlarmReceiver.java which in turn passes the String to RingtoneServiceProvider.java which creates the custom notification to be displayed. As it stands the string appears to be successfully passing into AlarmReceiver.java as I had a toast message display it before to see but when it goes to RingtoneServiceProvider.java it becomes "null".
This is part of FoodItems.java where the string value 'name' is a value entered by the user that I want to appear on the notification.
private void setAlarm(Calendar targetCal){
Toast.makeText(getApplicationContext(),
"Alarm is set for " + targetCal.getTime(),Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.putExtra("data",name);
intent.putExtra("ID", Alarmnum);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),Alarmnum,intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
This is AlarmReceiver.java
package com.example.kev00_000.kitchenhero;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String name = intent.getStringExtra("data");
int id = intent.getIntExtra("ID", 1);
Intent service_intent=new Intent(context, RingtonePlayingService.class);
service_intent.putExtra("data",name);
service_intent.putExtra("ID", id);
context.startService(service_intent);
NotificationManager notifications = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
}
}
And here is RingtonePlayingService.java
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
public class RingtonePlayingService extends Service {
MediaPlayer alarm;
private String name;
private static int NOTIFICATION_ID = 1;
@Nullable
@Override
public IBinder onBind(Intent intent) {
name = intent.getStringExtra("data");
NOTIFICATION_ID = intent.getIntExtra("ID", 1);
return null;
}
public void onCreate(){
super.onCreate();
alarm=MediaPlayer.create(this, R.raw.alarmclockbuzz);
alarm.setLooping(true);
Intent stopself = new Intent(this, StopAlarm.class);
PendingIntent pendingIntent
= PendingIntent.getBroadcast(this, 0, stopself, PendingIntent.FLAG_CANCEL_CURRENT);
final NotificationCompat.Builder notification
= new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setContentTitle("KitchenHero")
.setContentText("Time to put your "+name+" on!!")
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.addAction(R.drawable.ic_launcher, "STOP", pendingIntent);
Notification note=notification.build();
startForeground(NOTIFICATION_ID, note);
}
public int onStartCommand(Intent intent, int flags, int startId) {
alarm.start();
return START_NOT_STICKY;
}
public void onDestroy() {
alarm.stop();
alarm.release();
}
Upvotes: 1
Views: 71
Reputation: 339
Using Shared Preferences to do this
public static final String MyPREFERENCES = "MyPrefs" ;
SharedPreferences sharedpreferences;
private void setAlarm(Calendar targetCal){
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("name",name);
editor.putString("ID",ID);
editor.commit();
Toast.makeText(getApplicationContext(),
"Alarm is set for " + targetCal.getTime(),Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),Alarmnum,intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
And get String in other class
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("name", "");
Upvotes: 2