Summary of the problem that the Android application cannot receive the specified broadcast when it is not started


    Recently, I am making a requirement: the program does not have a desktop icon, and its plug-in can be automatically added to the Launcher after installation, that is, the program can complete some operations when it is not started.

    The solution that can be thought of is to statically register a broadcast in AndroidMainifest.xml, and monitor some broadcasts of the system to trigger the application to complete the operation, but the phenomenon is: after the program is installed, it cannot receive the broadcast of the system if it is not started; But after starting it once, the system broadcast can be received normally.

    By consulting the information, it is found that this problem only occurs in Android 3.1 and above versions. I used version 4.2.2 to test, and this problem will naturally occur. The specific reason is: starting from Android 3.1, the newly installed program will be placed in the "stopped" state, and will not change state until the program has been manually started at least once to receive the specified broadcast message normally. The purpose of Android is to prevent the broadcast from unintentionally or unnecessarily starting the background service of the app that is not started.

    That is to say, in Android 3.1 and above versions, it is impossible to complete some operations through the application itself when it is not started, but Android provides a way to send the specified Flag broadcast with the help of other applications, so that the application can be broadcast when the application is not started. In the case of still being able to receive the effect of the message.

    Starting from Android 3.1, the system defines two new Flags for the Intent, namely FLAG_INCLUDE_STOPPED_PACKAGES (indicating that it contains unstarted apps) and FLAG_EXCLUDE_STOPPED_PACKAGES (indicating that it does not contain unstarted apps), which are used to control whether the Intent should respond to the stopped state. The App works, and the specific operation methods are as follows:

1. Statically register the broadcast in the application that needs to receive the broadcast, define the action, and specify android:exported="true";

<receiver android:name=".receiver.UpdateWidgetReceiver"
     android:exported="true">
     <intent-filter>
          <action android:name="com.uperone.widget.action"/>
     </intent-filter>
</receiver>

2. Add the following code snippet to the application that sends the broadcast:

Intent intent = new Intent();
intent.setAction("com.uperone.widget.action");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
    This trick can be used to monitor the app or service started by the boot broadcast. If it is in the stopped state, the boot broadcast will not be received.


    According to this change, the pro test is effective! ! !















Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324944960&siteId=291194637