在我的 androidManifest 中,我的 mainActivity 几乎没有系统 Intent 过滤器。
有什么方法可以检测哪个 Intent 过滤器打开了我的 Activity ?
Android 服务也是如此 - 有办法检测吗?
我知道广播
有一个有 Intent 的 Actor ,我可以分析哪个 Intent 唤醒了它。
最佳答案
在 Activity 中,您可以使用 getIntent() 方法来检索用于启动该 Activity 的 Intent 。
public Intent getIntent()
Return the intent that started this activity.
在您的服务中,您可以通过 onStartCommand(..) 方法中的参数检查收到的 Intent :
public int onStartCommand (Intent intent, int flags, int startId)
然后,在这两种情况下,通过您的“调用者” Intent ,您可以比较其值(例如操作),以检查使用了哪个过滤器:
intent.getAction().equals(YOUR_FILTER_ACTION);
或者,如果您有其他要比较的 Intent ,可以使用 filterEquals() 方法:
intent.filterEquals(anotherIntent);
希望有帮助。
关于java - Android 服务或 Activity 可以知道哪个 Intent 打开了它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21429436/