【问题标题】:only last pending intent being set仅设置最后一个待处理的意图
【发布时间】:2014-04-05 03:52:13
【问题描述】:

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

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





  }

 }
}

【问题讨论】:

    标签: android android-pendingintent


    【解决方案1】:

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

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

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

    在您的循环中,Intents 是相同的(从过滤的角度来看),因此 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)。

    【讨论】:

    • 但我确实有不同的请求代码我增加计数
    • 抱歉,错过了count。并且只有循环中的最后一个 PendingIntent 集被传递?
    • 正确的@String 让我发布适用于实际通知的 AlarmService.class
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 2019-12-04
    相关资源
    最近更新 更多