0
我使用了一项服务来更新一个appwidget,并且还安排了定期更新的重复警报(即它仍然调用服务类)。我现在的问题是,我不知道如何取消报警,并在主屏幕上删除appwidget时停止服务。我已经尝试取消appwidget的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);
}
请你能指出,如果我做错了什么:
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()
呢?谢谢。
只是一个侧面说明,已经离开那些评论代码,只是为了让你知道我也试过了。
gosh谢谢,我现在可以享受我的星期日,我想知道我做错了什么。顺便说一句,如果我可能会问,你认为它是一个很好的设计或想法,在服务类中安排重复报警。我试过在配置类中做,但从未发生过。我不知道它是否因为配置类调用这个服务类。 – user712109 2011-04-17 13:20:35
@ user712109:对不起,我无法在摘要中回答这个问题。我不知道“配置类”是什么,等等。 – CommonsWare 2011-04-17 13:31:18
哦,对不起,我的意思是用户设置配置类的更新率,这就是警报如何获得计划的价值;来自首选项,因为配置类必须从服务类中最初更新小部件。但因为它不是问题的一部分,所以不会因此而打扰你。再次感谢 – user712109 2011-04-17 13:54:22