多状态栏通知,唯一ID无效

时间:2013-03-16 22:40:45

标签: android alarmmanager android-pendingintent notificationmanager

我有一个应用程序,它会向状态栏发送通知,作为提醒您服用避孕药。如果需要同时服用两粒药丸。然后发送两个单独的通知。我知道你需要在.notify(int id,notification notification)函数中使用一个唯一的id。然而,它似乎并没有起作用。我知道我的ids是不同的,因为我通过展示祝酒来测试它们。我将不胜感激任何建议。继承我的代码:

警报类:

DatabaseHelper notiDb = new DatabaseHelper(this);
    notiDb.open();
    final String dataName = notiDb.getDataName(pushed_name);
    String dataDaily = notiDb.getDataDaily(pushed_name);
    String dataWeekly = notiDb.getDataWeekly(pushed_name);
    String dataTwice = notiDb.getDataTwice(pushed_name);
    String dataDosage = notiDb.getDataDosage(pushed_name);
    String dataStart = notiDb.getDataStart(pushed_name);
    String dataID = notiDb.getDataID(pushed_name);
    notiDb.close();

    ID = Integer.parseInt(dataID);
    Calendar uncalendar = Calendar.getInstance();
    String unID = dataID +uncalendar;


    Toast toast=Toast.makeText(this, "Alarm class, Notification for " +dataName +" has been set id: " +ID, Toast.LENGTH_LONG);  
    toast.show();

   Intent intent_unique = new Intent(this, NotiScenario.class);
   intent_unique.putExtra("ID", ID);
   intent_unique.setData(Uri.parse(intent_unique.toUri(Intent.URI_INTENT_SCHEME)));
   PendingIntent pIntent = PendingIntent.getActivity(this, ID, intent_unique, 2);


    // Build notification
    Notification noti = new Notification.Builder(this)
        .setContentTitle("MedScan")
        .setContentText("3. You should take "+dataDosage +" pills of " +dataName)
        .setSmallIcon(R.drawable.original)
        .setContentIntent(pIntent)
       .build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    noti.defaults |= Notification.DEFAULT_ALL;
     //Hide the notification after its selected
   noti.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(ID, noti);

信息类:

DatabaseHelper notiDb = new DatabaseHelper(this);
    notiDb.open();
    final String dataName = notiDb.getDataName(pushed_name);
    String dataDaily = notiDb.getDataDaily(pushed_name);
    final String dataWeekly = notiDb.getDataWeekly(pushed_name);
    String dataTwice = notiDb.getDataTwice(pushed_name);
    final String dataDosage = notiDb.getDataDosage(pushed_name);
    String dataStart = notiDb.getDataStart(pushed_name);
    String dataID = notiDb.getDataID(pushed_name);
    notiDb.close();

     ID = Integer.parseInt(dataID);
     int value_Weekly = Integer.parseInt(dataWeekly);
     int value_Daily = Integer.parseInt(dataDaily);
     int value_Twice = Integer.parseInt(dataTwice);
     int value_Start = Integer.parseInt(dataStart);

     Calendar calendar = Calendar.getInstance();
     calendar.set(Calendar.HOUR_OF_DAY, value_Start);
     calendar.set(Calendar.MINUTE, 46);
     calendar.set(Calendar.SECOND, 00);
     int setTime = (value_Daily*value_Twice)/value_Weekly;



     Intent intent_noti = new Intent(this,Alarm.class);
     intent_noti.putExtra("ID", ID);
     intent_noti.setData(Uri.parse(intent_noti.toUri(Intent.URI_INTENT_SCHEME)));
     PendingIntent pendingIntent = PendingIntent.getActivity(this, ID, intent_noti, PendingIntent.FLAG_ONE_SHOT);
     AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
     am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), setTime, pendingIntent);

1 个答案:

答案 0 :(得分:3)

问题不在于您的Alarm类中,它会在Info类中创建通知,该类会创建一个警报以定期调用Info类。 创建警报待处理意图的两行是:

Intent intent_noti = new Intent(this,Alarm.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, ID, intent_noti, PendingIntent.FLAG_CANCEL_CURRENT);

因为无论您的ID如何,Intent都是相同的,因此新创建的PendingIntent将取消之前创建的PendingIntent。 为了使代码工作,根据Intent.filterEquals()方法,Intent必须不同:http://developer.android.com/reference/android/content/Intent.html#filterEquals(android.content.Intent)

要获得一个独特的意图,你可以这样做:

intent_noti.setAction(""+System.currentTimeMillis());

或:

intent_noti.putExtra("ID", ID);
intent_noti.setData(Uri.parse(intent_noti.toUri(Intent.URI_INTENT_SCHEME)));