【问题标题】:In which thread does an IntentService launched by a Notification action run?Notification 操作启动的 IntentService 在哪个线程中运行?
【发布时间】:2014-11-04 23:05:49
【问题描述】:

我有一个包裹在 PendingIntent 中的 IntentService,我想在用户点击特定通知操作时启动它,如下所示:

Intent actionIntentService = new Intent(context, ActionIntentService.class);
PendingIntent actionPendingIntent = PendingIntent.getService(app, getNotificationId(), actionIntentService, PendingIntent.FLAG_CANCEL_CURRENT);

然后,我确保将具有此待处理意图的操作添加到通过 NotificationCompat.Builder 创建的通知中:

notificationBuilder.addAction(R.drawable.ic_notif_action, actionTitle, actionPendingIntent);

我的问题是,当

onHandleIntent(Intent intent)

调用了我的 IntentService 的方法,它在哪个线程上运行?

我已经调试了我的代码,它给我的印象是这个方法调用在主线程上运行。如果是这种情况,我该怎么做才能让它在后台线程上运行?

编辑:

我的 ActionIntentService 确实扩展了 IntentService

class ActionIntentService extends IntentService

【问题讨论】:

  • IntentService 在主 UI 线程之外运行(这就是重点)

标签: android multithreading android-intent android-notifications intentservice


【解决方案1】:

请参考文档,您描述的方法不存在。你应该扩展IntentService并实现onHandleIntent(Intent)

文档说:

工作线程上调用此方法并请求处理。

【讨论】:

  • 我的问题是为什么 onHandleIntent() 在通过点击通知中的操作调用时在主线程上运行。
  • 它没有。你怎么知道?你是怎么检查的?
  • 放一个断点,进入线程选项卡,线程的名称是“main”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-16
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 2016-12-15
  • 2014-08-13
相关资源
最近更新 更多