在我的应用程序中,警报会创建一个有效的通知,但我正在尝试将字符串值从我的主类FoodItems.java传递到AlarmReceiver.java,后者又将String传递给RingtoneServiceProvider.java,后者创建自定义通知显示。因为它看起来成功传递到AlarmReceiver.java,因为我有一个toast消息显示它之前看到它但是当它转到RingtoneServiceProvider.java它变成" null"。
这是FoodItems.java的一部分,其中字符串值为' name'是用户输入的值,我想在通知中显示。
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);
}
这是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);
}
}
这是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();
}
答案 0 :(得分:2)
使用共享首选项执行此操作
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);
}
在其他类中获取String
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("name", "");