gpt4 book ai didi

android - 使用 AlarmManager 重新安排 IntentService - Intent 额外信息被忽略

转载 作者:太空宇宙 更新时间:2023-11-03 11:51:35 24 4
gpt4 key购买 nike

我有一个最初通过点击按钮启动的 IntentService:

        Intent intent = new Intent(this, ProximityService.class);
intent.putExtra("com.hybris.proxi.triggerTime", 5000l);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
long trigger = System.currentTimeMillis() + (5*1000);
alarmManager.set(AlarmManager.RTC_WAKEUP, trigger, pendingIntent);

这很好用。但是后来我试图从 intentservice 中再次执行相同的 intentservice。这里的想法是我可以改变下一次执行时间以节省电量。

@Override
protected void onHandleIntent(Intent intent) {
Log.d("demo", "ProximityService... STARTING");
Log.d("demo", "Intent has extra with key com.hybris.proxi.triggerTime" + intent.hasExtra("com.hybris.proxi.triggerTime"));

long lastTriggerTime = intent.getLongExtra("com.hybris.proxi.triggerTime", -1);
Log.d("demo", "Last trigger time: " + lastTriggerTime);

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

//trigger me again - TODO change this based on closeness to store
//TODO does not work, somehow triggertime is not incremented
if (lastTriggerTime < 50000)
{
long newTriggerTime = lastTriggerTime * 2;

Intent serviceIntent = new Intent(this, ProximityService.class);
serviceIntent.putExtra("com.hybris.proxi.triggerTime", (long)newTriggerTime);
Log.d("demo","New triggerTime set in new intent is : " + serviceIntent.getLongExtra("com.hybris.proxi.triggerTime", -1));

PendingIntent pendingIntent = PendingIntent.getService(this, 0, serviceIntent, 0);
long trigger = System.currentTimeMillis() + newTriggerTime;
alarmManager.set(AlarmManager.RTC_WAKEUP, trigger, pendingIntent);
Log.d("demo", "Alarm was set with trigger time " + newTriggerTime);
}
else {
Log.d("demo", "Not rescheduling alarm: lastTriggerTime is " + lastTriggerTime);
}


Log.d("demo", "ProximityService... DONE");

}

问题是

intent.getLongExtra("com.hybris.proxi.triggerTime", -1);

始终返回初始值 5000 - 因此下一个警报不会安排在不断增加的触发时间。不知何故,新触发时间的 Intent 额外设置不起作用......

有什么想法吗?

最佳答案

您需要使用 PendingIntent.FLAG_UPDATE_CURRENT

更改您的代码PendingIntent pendingIntent = PendingIntent.getService(this, 0, serviceIntent, 0);

PendingIntent pendingIntent = PendingIntent.getService(this,
0, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

关于android - 使用 AlarmManager 重新安排 IntentService - Intent 额外信息被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12799367/

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