android - 如何判断哪个Activity启动了某个服务?

标签 android android-intent android-service

我正在制作一个 Android 应用程序插件。我试图使其尽可能通用,但这给我带来了一个问题。我正在设计该插件,以便我将来制作的任何应用程序都可以轻松实现该插件。我遇到的问题是该插件包含一项服务,该服务会定期通过通知提醒用户。当用户打开通知栏并触摸通知时,需要打开最初启动该服务的 Activity。

使用以下代码启动服务:

    startService(new Intent(this, myService.class));

该服务使用以下代码启动通知:

    Intent notificationIntent = new Intent(this, myClass.class);

目前,myClass.class 已硬编码到通知中。这意味着,无论我制作多少个不同的应用程序,如果我使用此插件,我都必须始终将启动服务的类命名为“myClass”。

我想要一种通用的方法来获取启动服务的原始 Activity 。

最佳答案

I would like a universal way to get the original activity that started the service.

将您所需的信息放入与 startService() 一起使用的 Intent 中的 Intent extras 中。服务无法确定哪个 Activity 启动了它,很大程度上是因为服务不必由 Activity 启动...

关于android - 如何判断哪个Activity启动了某个服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9824398/

相关文章:

android - 在 retrofit 2 和 gson 中无法获得特殊字符

android - react-native run-android android-sdk 不存在

android - 在 Android 中从 Intent 引发时避免黑屏

Android in app billing 声称不支持使用示例代码

android - 使用异步任务在实时 Firebase 中提交大量值?

android - 在没有选择器的情况下使用标准 gmail 应用程序发送电子邮件

android - 从服务运行 Intent DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN

java - AlarmManager 从不在 AlarmReceiver/BroadcastReceiver 中调用 onReceive

android - 预定的警报管理器不工作 Android

android - 将 ListFragment 与带有 Holder 模式的 SimpleCursorAdapter 结合使用