我正在制作一个 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/