如何取消服务中的警报?
我使用服务来更新应用程序小部件,并为定期更新安排了重复警报(即它仍然调用服务类)。我现在的问题是,当应用程序小部件从主屏幕删除时,我不知道如何取消警报并停止服务。我尝试使用创建警报的相同挂起意图来取消应用程序小部件的 onDeleted() 中的警报,但它没有取消它。
这是服务计划的代码:
Intent widgetUpdate = new Intent();
widgetUpdate.setClass(this, appService.class);
//widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
widgetUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{appWidgetId});
//widgetUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Uri data = Uri.withAppendedPath(Uri.parse(URI_SCHEME + "://widget/id/"),
String.valueOf(appWidgetId));
widgetUpdate.setData(data);
PendingIntent newpending = PendingIntent.getService(this, 0, widgetUpdate,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime()+ updateRate, updateRate, newpending);
然后在appWidgetProviderClass的onDeleted()中:
public void onDeleted(Context context, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
//cancel the alarm
Intent widgetUpdate = new Intent();
//widgetUpdate.setClassName(this, appService.class);
//Uri data = Uri.withAppendedPath(Uri.parse(URI_SCHEME + "://widget/id/"),
// String.valueOf(appWidgetId));
//widgetUpdate.setData(data);
PendingIntent newpending = PendingIntent.getService(context, 0, widgetUpdate,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm =
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(newpending);
//cancel the service
context.stopService(new Intent(context,WeatherService.class);
}
super.onDeleted(context, appWidgetIds);
}
请您指出我是否做错了什么?谢谢。
只是一个旁注,留下了那些评论代码,只是为了让你们知道我也尝试过。
I used a service to update an appwidget and also scheduled a repeating alarm for periodic updates (i.e it still calls the service class). my problem now is, I don't know how to cancel the alarm and stop the service when an appwidget is deleted from the home screen. I have tried cancelling the alarm in onDeleted() of the appwidget with the same pending intent that created the alarm, but its not cancelling it.
Here is the code for the service schedule:
Intent widgetUpdate = new Intent();
widgetUpdate.setClass(this, appService.class);
//widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
widgetUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{appWidgetId});
//widgetUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Uri data = Uri.withAppendedPath(Uri.parse(URI_SCHEME + "://widget/id/"),
String.valueOf(appWidgetId));
widgetUpdate.setData(data);
PendingIntent newpending = PendingIntent.getService(this, 0, widgetUpdate,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime()+ updateRate, updateRate, newpending);
then in the onDeleted() of appWidgetProviderClass:
public void onDeleted(Context context, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
//cancel the alarm
Intent widgetUpdate = new Intent();
//widgetUpdate.setClassName(this, appService.class);
//Uri data = Uri.withAppendedPath(Uri.parse(URI_SCHEME + "://widget/id/"),
// String.valueOf(appWidgetId));
//widgetUpdate.setData(data);
PendingIntent newpending = PendingIntent.getService(context, 0, widgetUpdate,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm =
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(newpending);
//cancel the service
context.stopService(new Intent(context,WeatherService.class);
}
super.onDeleted(context, appWidgetIds);
}
please could you point out if am doing anything wrong? Thanks.
just a side note, have left those commented codes, just to let you guys know i have tried that too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
PendingIntent
与cancel()
一起使用,该PendingIntent
与您与setRepeating()
一起使用的那个等效。换句话说:setRepeating()
Intent
上调用setClass()
,你需要调用同样的setClass()
on thecancel()
IntentsetRepeating()
Intent
上调用setAction()
,您需要在cancel()
Intent 上调用相同的setAction(
setRepeating( )
Intent
,你需要在cancel()
Intent 上调用相同的setData()
额外的内容并不重要,但是组件(类)、操作、数据 (
Uri
) 和 MIME 类型都很重要。You need to use a
PendingIntent
withcancel()
that is equivalent to the one you used withsetRepeating()
. In other words:setClass()
on thesetRepeating()
Intent
, you need to call the samesetClass()
on thecancel()
IntentsetAction()
on thesetRepeating()
Intent
, you need to call the samesetAction()
on thecancel()
IntentsetData()
on thesetRepeating()
Intent
, you need to call the samesetData()
on thecancel()
IntentExtras do not matter, but the component (class), action, data (
Uri
), and MIME type all matter.