android - 仅设置最后一个未决 Intent

标签 android android-pendingintent

我有一个广播接收器类,它进入数组列表并根据每个对象的时间设置多个未决 Intent ,尽管只有最后一个未决 Intent 集在启动后显示,我使用不同的计数值来确保请求代码是不同的,但只有在我的循环中设置的最后一个未决 Intent 仍然显示

public class AutoStartNotifyReceiver extends BroadcastReceiver {

private PendingIntent pendingIntent;
Calendar current = Calendar.getInstance();
ArrayList<appointment> myArray;

 private final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";

 @Override
 public void onReceive(Context context, Intent intent) {

  if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){


        FileInputStream input = null;
        ObjectInputStream inputob = null;
        try {
            input = context.getApplicationContext().openFileInput("app.ser");   
            inputob = new ObjectInputStream(input);

             myArray = (ArrayList<appointment>) inputob.readObject(); 

            inputob.close();
            input.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

      int count = 0;
      for(appointment cals: myArray)
      {
         if(cals.gettimeofappt()>current.getTimeInMillis())
         {
          count ++;
          Intent myIntent = new Intent(context, MyAlarmService.class);

          pendingIntent = PendingIntent.getService(context, count, myIntent, 0);


       AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);


       alarmManager.set(AlarmManager.RTC_WAKEUP, cals.gettimeofappt(), pendingIntent);    
         }
      }





  }

 }
}

最佳答案

PendingIntent 的本质是它们在这种情况下会被重用。来自 the docs :

A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it...

The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

在您的循环中,Intent 是相同的(从过滤的角度来看),因此 PendingIntent 被重用 - 您只能获得其中之一。

您的问题的解决方案在同一文档页面上:

If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent.filterEquals, or different request code integers supplied to getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).

关于android - 仅设置最后一个未决 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22876371/

相关文章:

android - PendingIntent 不会启动所需的 fragment

android - 某些使用电子邮件联系人选择器的用户出现异常

java - Firebase 可调用函数出错

java - 重构包含无参数方法的 switch 语句

android - AlarmManager - 任务队列(PendingIntent 队列)

android - 通知中的未决 Intent 未被清除

Android Studio 无法在 Ubuntu 上启动

android - Android 中的连接超时

android - PendingIntent 启动应用程序包的不同 Activity

android - 为什么我的 Intent 在我的应用程序的设置中打开应用程序信息,而不是我传递给它的 Activity 类?