androidandroid-intentandroid-sourceandroid-5.1.1-lollipop

Receive broadcasts in stopped application


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.


Solution

  • 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">