gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:10:58 25 4
gpt4 key购买 nike

我有一个广播接收器类,它进入数组列表并根据每个对象的时间设置多个未决 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com