android多个通知
问题描述:
嗨我是新来的android和我正在开发提醒,有三个类别的第一个是设置多个约会提醒和第二个多月(票据)提醒,第三个多个每日(医药)提醒都在同一个项目中。android多个通知
所有的工作正常,但我有一个问题,当我设置三个提醒,他们都出现在LogCat上,所有时间都会触发,但问题是当医药通知出现在酒吧通知和帐单提醒后, ,该账单通知将取代滑下通知中的现有药物提醒,反之亦然。约会提醒通知工作正常。
我需要通知所有三个类别的下滑通知,而不是用另一个替换现有的。
所以我认为问题是在通知的代码,但我无法弄清楚我的代码中有什么问题。
我对每个类别都有单独的提醒服务。有人可以看看它,并告诉我什么问题是?
这是服药服务
public class Medicines_ReminderService extends WakeReminderIntentService {
public Medicines_ReminderService() {
super("ReminderService");
}
@Override
void doReminderWork(Intent intent) {
Log.d("MedicinesReminderService", "Doing work.");
Long rowId = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID_MEDS);
NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, MedicinesEdit.class);
notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID_MEDS, rowId);
int N_Med_requestCode = rowId.intValue();
PendingIntent pi = PendingIntent.getActivity(this, N_Med_requestCode, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification note=new Notification(android.R.drawable.stat_sys_warning, getString(R.string.notify_new_reminder_message), System.currentTimeMillis());
note.setLatestEventInfo(this, getString(R.string.notify_new_medicine_reminder_title), getString(R.string.notify_new_reminder_message), pi);
note.defaults |= Notification.DEFAULT_SOUND;
note.flags |= Notification.FLAG_AUTO_CANCEL;
int id = (int)((long)rowId);
mgr.notify(id, note);
}
}
这是该法案提醒服务
public class Bills_ReminderService extends WakeReminderIntentService {
public Bills_ReminderService() {
super("ReminderService");
}
@Override
void doReminderWork(Intent intent) {
Log.d("BillsReminderService", "Doing work.");
Long rowId = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID_BILLS);
NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, BillsEdit.class);
notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID_BILLS, rowId);
int N_Bill_requestCode = rowId.intValue();
PendingIntent pi = PendingIntent.getActivity(this, N_Bill_requestCode, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification note=new Notification(android.R.drawable.stat_sys_warning, getString(R.string.notify_new_reminder_message), System.currentTimeMillis());
note.setLatestEventInfo(this, getString(R.string.notify_new_bill_reminder_title), getString(R.string.notify_new_reminder_message), pi);
note.defaults |= Notification.DEFAULT_SOUND;
note.flags |= Notification.FLAG_AUTO_CANCEL;
int id = (int)((long)rowId);
mgr.notify(id, note);
}
}
在此先感谢
最好的问候, zoza
答
您正在使用行ID作为它的“唯一”标识符在你拥有的3项服务中,并不是唯一的。
即
int id = (int)((long)rowId);
mgr.notify(id, note);
从文档在http://developer.android.com/guide/topics/ui/notifiers/notifications.html
管理您的通知“的ID唯一从你的应用程序中标识的通知。该ID是必要的,如果你需要更新的通知或(如果您的应用程序管理不同类型的通知)当用户通过通知中定义的意图返回到您的应用程序时,请选择适当的操作。“
如果你只需要一个通知每个类别中,你可以使用:
int BillsID=1;
int MedsID=2;
mgr.notify(BillsID, note);
mgr.notify(MedsID, note);
,或者如果你真的需要每个通知是基于它的行号,你需要添加一个微分避免同ID被分配给单独的通知。
int BillsID=100000;
int MedsID=200000;
mgr.notify(BillsID+RowID, note);
mgr.notify(MedsID+RowID, note);