Method of Self startup after Installation by Android Application

  • 2021-10-11 19:41:11
  • OfStack

Like most methods 1 on the Internet, broadcast means are used:

ACTION_PACKAGE_ADDED 1 A new application package has been installed on the device. The data includes the package name (the latest installed package program cannot receive this broadcast)

ACTION_PACKAGE_REPLACED 1 A new version of the application is installed on the device, replacing the previous version

ACTION_PACKAGE_CHANGED 1 Existing application package has been changed, including package name

ACTION_PACKAGE_REMOVED 1 An existing application package has been removed from the device, including the package name (the package being installed cannot receive this broadcast)

ACTION_PACKAGE_RESTARTED If the user restarts a package, all processes in the package will be killed, and all runtime states associated with it should be removed, including the package name. (The restart package program cannot receive this broadcast.)

ACTION_PACKAGE_DATA_CLEARED the user has cleared the data of 1 packet, including the packet name (the packet cleaner cannot receive this broadcast)

Direct idea: Register the broadcast to receive the action required above.

However, after Android 3.1, there are the following mechanisms:

force-stop in Manage Application of Settings makes App in a stopped state!

Here is what Google describes

What is Stopped State


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.

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.

Why Android Adds this


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.

As the above references point out it will prevent broadcast intents delivering to stopped packages. Actually this control mechanism will ensure safety and save energy.

Android 3.1 APIs

Translation:

The "Forced Stop" function in System Settings-Application Management is to put app in the (stopped) stop state.

The following is the official description of google:

What is a stop state?

Beginning with Andriod 3.1, the System Package Management Service tracks app in a stalled state and provides a way to control their launch from a daemon or other application.

Note: The stop state of the application is different from the stop state of activity (active). The system handles these two types of stop states separately.

Why does Android add this feature?

Note: The FLAG_EXCLUDE_STOPPED flag is added by default for all Intent used to send broadcasts. This is done to prevent a broadcast sent from the background service from accidentally starting a component that has been stopped. 1 background service service or app application can

Override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to the broadcast Intent object, so that the broadcast can activate the application in the stopped state.

The above description indicates that the system will prevent the stopped app from receiving broadcasts by default. The purpose of this control mechanism is to ensure safety and save electricity.

Therefore, to realize self-startup after installing apk, the premise is that

1. Trigger the ACTION_PACKAGE_REPLACED broadcast (that is, the apk overlay replacement installation received, and the first installed broadcast ACTION_PACKAGE_ADDED will not be triggered by the current installation package because the app has not been run)

2. Use static registration broadcast in app project (because dynamic broadcast can only be accepted after app runs)

3. app has ever been run (i.e. not in stopped state)

Test on Android5.1 real machine:

The first installation of app does not trigger broadcasting.

Overwrite the installation of app that has not been run, and will not trigger the broadcast

After installing and running app, exit App (click the return key and remove it from the recent task. At this time, check in Settings-Application, app is still not in stop state). After overlay installation, app runs automatically successfully. (It can be seen as implementing self-startup after installation)

At this point, exit App and "force stop" app in Settings-Application. app does not run automatically after override installation. (View in Settings-Apply, app is in stop)

Therefore, as long as the App is running, the apk can be directly overwritten and installed, and the broadcast receiver can be used to realize self-startup after installation. (At least on android 5.1 ^, ^)

The following is a brief introduction to the following code:

(1) Custom Broadcast Receiver:


public class MyReceiver extends BroadcastReceiver {
 
	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		String localPkgName = context.getPackageName();// Acquire MyReceiver In App Package name of 
		Uri data = intent.getData();
		String installedPkgName = data.getSchemeSpecificPart();// Obtain the installed Apk The package name of, only in the app Override self-start after installation 
		if((action.equals(Intent.ACTION_PACKAGE_ADDED)
				|| action.equals(Intent.ACTION_PACKAGE_REPLACED)) && installedPkgName.equals(localPkgName)){
			Intent launchIntent = new Intent(context,MainActivity.class);
			launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(launchIntent);
		}
	}
}

(2) Statically registered broadcast receiver in AndroidManifest. xml


<application
  android:allowBackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/AppTheme" >
  <activity
   android:name="com.example.mydemo.MainActivity"
  	android:configChanges="orientation|keyboard"
   android:label="@string/app_name">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
  <receiver android:name="com.example.mydemo.MyReceiver">
   <intent-filter>
    <action android:name="android.intent.action.PACKAGE_ADDED" /> 
    <action android:name="android.intent.action.PACKAGE_REPLACED" />
    <action android:name="android.intent.action.PACKAGE_REMOVED" />
    <data android:scheme="package"/>
   </intent-filter>
  </receiver>
 </application>

Related articles: