Receive broadcasts in stopped application

可紊 提交于 2020-01-02 08:22:51

问题


By default, an application which in stopped state (not yet run even once) could not receive broadcasts. There is a way to receive it if we add the flag FLAG_INCLUDE_STOPPED_PACKAGES to the broadcast intent.

My problem is that I need to receive system intent android.intent.action.MEDIA_MOUNTED (and I don't want to add that flag to it).

How can I receive it even in stopped application?

It seems to me that for sure it's possible because for example com.android.shell application is able to receive such intents even if it's force stopped. How it does it?


EDIT:

Just to clarify, because it appeared that this is not obvious:

Everything here is from the point of view of Android Open Source Project developer. I'm modifying the operating system source code and the application which I want to receive the broadcast could be preinstalled, signed with platform certificate, it can use hidden API, etc. Everything is permitted, even modifications of the system.


回答1:


Declaring your application as a system app should allow you to receive regular broadcasts even if your app is in "stopped state".

Add the following to your manifest and make sure your application is signed by the platform key:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
...
android:sharedUserId="android.uid.system">



回答2:


If you are on a rooted device you can use Xposed to modify android.content.Intent#isExcludingStopped, as such:

public class DontExcludeStoppedPackagesFromIntents implements IXposedHookLoadPackage {
    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        if (lpparam.packageName.equals("android")) {
            XposedBridge.log("In package: " + lpparam.packageName);

            findAndHookMethod("android.content.Intent", lpparam.classLoader, "isExcludingStopped", new XC_MethodReplacement() {
                @Override
                protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
                    return false;
                }
            });
        }
    }
}

Don't forget to put the class name in xposed_init. Of course this has some security implications, effectively taking you back to Android 3.0-, though this shouldn't be much of an issue because noone expects to exploit a vulnerability that is not present on nearly all existing Android devices that do no have this patch applied.

I tried the above and it works!



来源:https://stackoverflow.com/questions/38490317/receive-broadcasts-in-stopped-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!