android多个通知

时间:2011-12-04 21:06:36

标签: android notifications

嗨,我是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

1 个答案:

答案 0 :(得分:4)

您正在使用行ID作为“唯一”标识符,因为它可能在您拥有的3项服务之间不是唯一的。

    int id = (int)((long)rowId);
    mgr.notify(id, note);

来自http://developer.android.com/guide/topics/ui/notifiers/notifications.html

的文档

管理您的通知“该ID唯一标识您应用程序中的通知。如果您需要更新通知或(如果您的应用程序管理不同类型的通知)选择适当的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);