我正在制作可与AlarmManager一起使用的应用程序。 我使用BroadcastReceiver来接收警报。 但是首先,我在主活动中调用方法setAlarms()。 我已经检查了断点,并且传递给broadcastreceiver的值不为null,但是当我尝试在BroadcastReceiver本身中获取它们时,它将返回null。 这是我的代码:
//MainActivity
public void SetAlarms()
{
ArrayList<TaskItem> dates = TaskUtils.getInstance().getDatedTasksInMillis(this);
ArrayList<TaskItem> repeating = TaskUtils.getInstance().getRepeatingTasksInMillis(this);
Intent intent;
PendingIntent pi;
AlarmManager manager;
for (int i = 0; i < dates.size(); i++)
{
TaskItem current = dates.get(i); //TASK ITEM IS THE CLASS WHERE I GET THE VALUES
long millis = current.getDateInMillis();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent = new Intent(getApplicationContext(), NotificationReceiver.class);
Bundle b = new Bundle();
b.putInt("ID", current.getID());
b.putString("Title", current.getDescription());
b.putString("Description", current.getDescription());
b.putInt("Tag", current.getTag());
intent.putExtras(b);
long minus = 1000 * 60 * current.getRemind();
millis -= minus;
pi = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
manager = (AlarmManager)getSystemService(ALARM_SERVICE);
manager.setExact(AlarmManager.RTC_WAKEUP, millis, pi);
}
}
}
//BroascastReceiver Class
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//THIS METHOD ACTIVATES WHEN THE ALARMMANAGER GETS TO CERTAIN TIME
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle b = i.getExtras();
int id = b.getInt("ID");
String title = b.getString("Title");
String description = b.getString("Description");
int tag = b.getInt("Tag");
//BUILDING NOTIFICATION
NotificationHelper helper = new NotificationHelper(context);
NotificationCompat.Builder nb = helper.getTaskChannelNotification(id, title, description, tag);
int RandomNumber = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
helper.getManager().notify(RandomNumber, nb.build());
}
答案 0 :(得分:1)
在NotificationReceiver中,您尝试从新创建的意图中读取附加内容,而不是在onReceive方法中将其作为参数接收。
应该改为intent.getExtras()而不是i.getExtras()