我已经实现了类似的东西。在我的应用程序中 aBroadcastReceiver
收到警报 (an Intent
),然后开始传递一些参数的活动。
这是我的一段代码。让它成为关于传递和接收参数的部分
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context c, Intent i) {
int answerType = i.getExtras().getInt("answerType");
Intent intent = new Intent(arg0, ReminderActivity.class);
intent.putExtra("id", i.getExtras().getInt("id"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startActivity(intent);
}
}
我编写了一个名为 ReminderManger 的类,其中包含一个设置警报的方法。像这样的东西:
public void setAlarm(Notification n){
Intent intent = new Intent(reminderContext, AlarmReceiver.class);
intent.putExtra("id",n.ID);
intent.putExtra("answerType",n.AnswerType);
int execTimeInt = Integer.parseInt(n.ExecutionTimespan);
long execTime = (long)execTimeInt;
execTime = execTime*1000;
PendingIntent pendingIntent = PendingIntent.getBroadcast(reminderContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
currentPendingIntent = pendingIntent;
AlarmManager alarmManager = (AlarmManager)reminderContext.getSystemService(reminderContext.ALARM_SERVICE);
currentAlarm = alarmManager;
if(execTime < System.currentTimeMillis() && System.currentTimeMillis() - execTime < NOTIFICATION_THRESHOLD)
{
alarmRunning = true;
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+EXECUTION_DELAY,pendingIntent);
}
}