0

我有一个应用程序可以向状态栏发送通知以提醒吃药。如果需要同时服用两粒药。然后将发送两个单独的通知。我知道您需要在 .notify(int id, notification notification) 函数中使用唯一的 id 才能发生这种情况。但是,它似乎不起作用。我确定我的 id 是不同的,因为我通过显示敬酒来测试它们。我将不胜感激任何建议。这是我的代码:

报警类:

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);
4

1 回答 1

4

问题不在创建通知的警报类中,而是在创建警报以定期调用 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 只会取消先前创建的。为了使您的代码正常工作,Intent 必须根据 Intent.filterEquals() 方法有所不同:http: //developer.android.com/reference/android/content/Intent.html#filterEquals (android.content.Intent )

要获得独特的 Intent,您可以执行以下操作:

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

或者:

intent_noti.putExtra("ID", ID);
intent_noti.setData(Uri.parse(intent_noti.toUri(Intent.URI_INTENT_SCHEME)));
于 2013-03-17T01:23:16.280 回答