Mah*_*esh 8 android android-pendingintent
我试图在一段时间间隔后显示不同的通知但发生的事情是它更新当前PendingIntent的新的一个因此我得到只有一个通知,即使我触发4 - 5个待处理的意图请求
在按钮上单击我执行以下操作
try{
adapter.OpenDB();
int id = adapter.getMaxId("reminder")+1;
adapter.CloseDB();
Intent intent = new Intent(MilestonesActivity.this, AlarmReceiver.class);
intent.putExtra("message", ""+editMsg.getText()+" "+editDate.getText());
intent.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MilestonesActivity.this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP,
reminddate.getTimeInMillis(), pendingIntent);
adapter.CloseDB();
}catch (Exception e) {
// TODO: handle exception
}
Run Code Online (Sandbox Code Playgroud)
AlarmReceiver.java
@Override
public void onReceive(Context context, Intent intent) {
Intent i =new Intent(context, NotificationService.class);
try{
int id = intent.getExtras().getInt("id");
i.putExtra("id", id);
CharSequence message = intent.getExtras().getString("message");
i.putExtra("message", message);
}catch (Exception e) {
// TODO: handle exception
}
context.startService(i);
}
Run Code Online (Sandbox Code Playgroud)
NotificationService.java
public void show(Intent intent) {
try{
NotificationManager nm;
Log.e("recieve", "ok");
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Lawyer app Reminder";
CharSequence message = intent.getExtras().getString("message");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(),
PendingIntent.FLAG_ONE_SHOT);// also tried FLAG_UPDATE_CURRENT
int id =intent.getExtras().getInt("id");
Log.e("I", ""+id);
Notification notif = new Notification(R.drawable.message_active,
"Lawyer app Reminder.", System.currentTimeMillis());
notif.defaults = Notification.DEFAULT_ALL;
notif.flags = Notification.FLAG_AUTO_CANCEL;
notif.setLatestEventInfo(this, from, message, contentIntent);
nm.notify(id, notif);
}catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
plaese提前帮助我.谢谢
Mah*_*esh 37
我找到了
pendingintent构造函数的第二个参数不应该相同(这是0硬编码)
我将其更改为(int)System.currentTimeMillis(),以便它不应该是相同的:)
Intent ni =new Intent(NotificationService.this,ModifyDatabaseService.class);
ni.putExtra("id", ""+id);
PendingIntent contentIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), ni,
PendingIntent.FLAG_ONE_SHOT);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7207 次 |
| 最近记录: |