Android 仅设置最后一个挂起的意图
我有一个broadcast receiver类,它进入arraylist,并根据每个对象的时间设置多个挂起的意图,虽然在引导后只有最后一个挂起的意图集显示,但我使用不同的count值来确保请求代码不同,但只有我循环中的最后一个挂起的意图集仍然显示Android 仅设置最后一个挂起的意图,android,android-pendingintent,Android,Android Pendingintent,我有一个broadcast receiver类,它进入arraylist,并根据每个对象的时间设置多个挂起的意图,虽然在引导后只有最后一个挂起的意图集显示,但我使用不同的count值来确保请求代码不同,但只有我循环中的最后一个挂起的意图集仍然显示 public class AutoStartNotifyReceiver extends BroadcastReceiver { private PendingIntent pendingIntent; Calendar current = Cale
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);
}
}
}
}
}
公共类AutoStart NotifyReceiver扩展了BroadcastReceiver{
私人吊挂帐篷;
当前日历=Calendar.getInstance();
ArrayList myArray;
私有最终字符串BOOT\u COMPLETED\u ACTION=“android.intent.ACTION.BOOT\u COMPLETED”;
@凌驾
公共void onReceive(上下文、意图){
if(intent.getAction().equals(启动完成动作)){
FileInputStream输入=null;
ObjectInputStream inputob=null;
试一试{
input=context.getApplicationContext().openFileInput(“app.ser”);
InputB=新对象InputStream(输入);
myArray=(ArrayList)inputob.readObject();
inputob.close();
input.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}catch(classnotfounde异常){
e、 printStackTrace();
}
整数计数=0;
对于(约会CAL:myArray)
{
if(cals.getTimeOfApp()>current.getTimeInMillis())
{
计数++;
Intent myIntent=新的Intent(上下文,MyAlarmService.class);
PendingEvent=PendingEvent.getService(上下文,计数,myIntent,0);
AlarmManager AlarmManager=(AlarmManager)context.getSystemService(context.ALARM\u服务);
alarmManager.set(alarmManager.RTC_唤醒,cals.GetTimeOfApp(),PendingContent);
}
}
}
}
}
在这样的情况下,它们会被重用,这是pendingent
的本质。发件人:
PendingEvent本身只是对系统维护的令牌的引用,该令牌描述用于检索它的原始数据
用于匹配的Intent部分与Intent.filterEquals定义的部分相同。如果根据Intent.filterEquals使用两个等效的Intent对象,则两个对象将获得相同的PendingIntent
在循环中,Intent
s是相同的(从过滤的角度来看),因此pendingent
得到重用-您只得到其中一个
问题的解决方案位于同一文档页上:
如果确实需要多个不同的PendingEvent对象同时处于活动状态(例如用作同时显示的两个通知),则需要确保它们有不同之处,以便将它们与不同的PendingEvents关联。这可能是Intent.filterEquals考虑的任何Intent属性,也可能是提供给getActivity(Context,int,Intent,int)、getActivities(Context,int,Intent[],int)、getBroadcast(Context,int,Intent,int)或getService(Context,int,Intent,int)的不同请求代码整数
但是我有一个不同的请求代码,对不起,这里没有。只有循环中的最后一个PendingEvent集得到传递?正确的@String让我发布与实际通知一起工作的AlarmService.class