我正在为Android构建一个学习计划应用程序,我已设置AlarmManager
并在时间流逝时触发通知,这样可以正常工作。
设置警报时,警报详细信息保存到SQLite
数据库,当警报设置为关闭时,我需要调用警报信息,是否有某种形式的变量我可以保存到数据库中并链接到特定警报,以便我可以在SQLite query
中使用它来查找正确的条目。
以下更新代码:
Long nIdLong = System.currentTimeMillis();
String nId = nIdLong.toString();
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
alarmIntent.putExtra("nID", nId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent , 0);
AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(getActivity().ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
AlarmReciever:
public void onReceive(Context context, Intent intent)
{
long dbId = intent.getLongExtra("nID", 0);
String notificationId = Long.toString(dbId);
Log.i("App", "called receiver method");
try{
Toast.makeText(context, notificationId, Toast.LENGTH_LONG).show();
}catch(Exception e){
e.printStackTrace();
}
}
提前致谢
答案 0 :(得分:0)
设置闹钟时,首先检索将在通过
向SQLite插入闹钟详细信息时创建的IDlong uniqueId = db.insert(your query);
并将其置于PendindIntent for AlarmManager
的意图中Intent alarmIntent = new Intent(this, AlarmEventReceiver.class);
alarmIntent.putExtra("dbId", uniqueId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent , 0);
在AlarmEventReceiver的onReceive()中,你可以获得id&从DB中检索数据。
@Override
public void onReceive(Context context, Intent intent) {
long dbId = intent.getLongExtra("dbId", 0);
}
答案 1 :(得分:0)
你通过意图发送一个字符串,它需要一个Long,这个代码应该可以工作。
Long nIdLong = System.currentTimeMillis();
String nId = nIdLong.toString();
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
alarmIntent.putExtra("nID", nIdLong);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent , 0);
AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(getActivity().ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);