仅设置最后挂起的意图

时间:2014-04-05 03:52:14

标签: android android-pendingintent

我有一个广播接收器类进入arraylist并根据每个对象的时间设置多个挂起的意图,尽管在启动后只显示最后一个待处理的意图集我使用不同的count值来确保请求代码是不同的,但只有我的循环中设置的最后一个挂起的意图仍显示

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





  }

 }
}

1 个答案:

答案 0 :(得分:2)

它具有PendingIntent的性质,可以在这种情况下重复使用。来自the docs

  

PendingIntent本身只是对系统维护的令牌的引用,描述了用于检索它的原始数据......

     

用于匹配的Intent部分与Intent.filterEquals定义的部分相同。如果你使用两个与Intent.filterEquals相同的Intent对象,那么你将获得两个相同的PendingIntent。

对于你的循环,Intent是相同的(从过滤的角度来看),所以PendingIntent被重用 - 你只能得到其中一个。

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

  

如果您确实需要多个不同的PendingIntent对象同时处于活动状态(例如用作同时显示的两个通知),那么您需要确保关联它们的某些内容有所不同他们有不同的PendingIntents。这可能是Intent.filterEquals考虑的任何Intent属性,或者提供给getActivity(Context,int,Intent,int),getActivities(Context,int,Intent [],int),getBroadcast(Context,int)的不同请求代码整数。 ,Intent,int)或getService(Context,int,Intent,int)。