Many people have encountered problems that cannot be received by broadcasts, such as Google Play promotion and installation of broadcasts that have not been received, etc., etc., what are the reasons for these questions? This article will answer.
Starting from Android 3.1 (HoneyComb), that is, API 12, Android introduced a new set of startup controls, which is the program's stop state. Let's take a look at Google's description of the program's stop state.
What is the stop state of the program
Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications.
Starting from Android 3.1, the system's package manager begins tracking programs that process stop states. It provides methods to control the startup of them from background processes or other programs.
Note that an application's stopped state is not the same as an Activity's stopped state. The system manages those two stopped states separately.
Note that the program's stop state is different from the Activity's stop state, and the system will handle these two states separately.
The platform defines two new intent flags that let a sender specify whether the Intent should be allowed to activate components in stopped application. The Android platform provides two intent flags that allow the party sending the broadcast to decide whether the broadcast needs to be sent to the stopped program at the same time.
FLAG_INCLUDE_STOPPED_PACKAGES — Include intent filters of stopped applications in the list of potential targets to resolve against. Add already supported programs to the target processor that can handle intent.
FLAG_EXCLUDE_STOPPED_PACKAGES — Exclude intent filters of stopped applications from the list of potential targets. The target processor that can handle intent does not include a stopped program.
If there are no or two flags above intnet, the target processor contains the program that has been stopped. However, note that the system will add FLAG_EXCLUDE_STOPPED_PACKAGES flag to all broadcast intents.
Why Android introduces this state
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. A background service or application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications.
It should be noted that the system will add a FLAG_EXCLUDE_STOPPED_PACKAGES flag to all broadcast intents by default. The purpose of this is to prevent broadcasts from accidentally or starting unnecessary components of the program in a stopped state.
In general, intnet broadcasts, the receiver of programs in a stopped state cannot be accepted. So how can these programs in a stopped state be accepted? You can do this. When sending broadcasts in background services or applications, add a FLAG_INCLUDE_STOPPED_PACKAGES flag, which means that the program in a stopped state is included. This way, the program in a stopped state can be activated.
As the above quote points out, the system defaults to block broadcast intents from sending to program packages in a stopped state, which is actually necessary to ensure safety and power saving. For example, if a network-changing broadcast is registered and listens, and when it gets broadcast, it will do a series of network operations, which will inevitably consume energy.
Switch between active and stop states
The program will be in a stopped state when the program is first installed and not started, or if the user manually stops it from program management.
How to change to a stop state
1. Click Force Stop on the Application Details Page in Settings Application Management
2. Use adb shell adb shell am force-stop package-name
3. Use the hidden method of ActivityManager forceStopPackages, and add the permission to the manifest <uses-permission android:name=".FORCE_STOP_PACKAGES"/>
How to get out of the stop state
1. Start the program manually
2. Use adb to activate application components, such as activity or receiver
Send broadcast intent to the application in a stopped state
1. When the Java code sends the Intent, add flag FLAG_INCLUDE_STOPPED_PACKAGES
2. If you use adb, you also add FLAG_INCLUDE_STOPPED_PACKAGES (the specific value is 32), such as adb shell am broadcast -a .INSTALL_REFERRER -f 32
Check if it is in a stop state
1. Enter the detailed page of Settings - Application Management - An application. If the Force Stop button is not available, it means that the program is already in a stop state.
2. Enter the device terminal and view the system file cat /data/system/
Q&A session
Question: If my program does not have an activity but only a receiver, how can I activate it to receive normal broadcast intent?
Answer: In fact, if it is the case mentioned above, the app is not in a stop state after installation, because it has no direct click behavior for users to remove it from the stop state. You can receive broadcast intent normally unless you artificially force it to stop.
Question: Will the system program be stopped when it is just installed?
Answer: The system's programs are usually stored in the /system/app directory and will not be stopped after the initial installation.
Question: Google Play's promotional broadcast is said to be sent after the program is installed. Is it affected after 3.1?
Answer: Unaffected. Google documentation says that INSTALL_REFERRER will be sent after the program is installed. According to actual viewing of logs, after 3.1, it is sent when the program is opened for the first time after the program is installed.