如何取消服务中的警报?

发布于 2024-11-02 01:54:05 字数 1848 浏览 2 评论 0原文

我使用服务来更新应用程序小部件,并为定期更新安排了重复警报(即它仍然调用服务类)。我现在的问题是,当应用程序小部件从主屏幕删除时,我不知道如何取消警报并停止服务。我尝试使用创建警报的相同挂起意图来取消应用程序小部件的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声静候 2024-11-09 01:54:05

您需要将 PendingIntentcancel() 一起使用,该 PendingIntent 与您与 setRepeating() 一起使用的那个等效。换句话说:

  • 如果你在setRepeating() Intent上调用setClass(),你需要调用同样的setClass() on the cancel() Intent
  • 如果您在 setRepeating() Intent 上调用 setAction() ,您需要在 cancel() Intent 上调用相同的 setAction(
  • ) 如果您在 setRepeating( ) Intent,你需要在 cancel() Intent 上调用相同的 setData()

额外的内容并不重要,但是组件(类)、操作、数据 (Uri) 和 MIME 类型都很重要。

You need to use a PendingIntent with cancel() that is equivalent to the one you used with setRepeating(). In other words:

  • If you call setClass() on the setRepeating() Intent, you need to call the same setClass() on the cancel() Intent
  • If you call setAction() on the setRepeating() Intent, you need to call the same setAction() on the cancel() Intent
  • If you call setData() on the setRepeating() Intent, you need to call the same setData() on the cancel() Intent

Extras do not matter, but the component (class), action, data (Uri), and MIME type all matter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文