android startup self startup APP and test method using adb command

  • 2021-10-13 08:30:08
  • OfStack

The implementation of android startup and automatic operation of APP is actually very simple. When the android system is running, the system broadcast "android. intent. action. BOOT_COMPLETED" will be emitted, so we can listen to it and open APP. At present, most android mobile phone systems prohibit APP installed by the third party from starting up by default, and only the system APP (system/app) has this permission by default, so it is generally necessary to go to the security center or the mobile phone housekeeper to set it as allowed. Here is a digression. Unless there are special circumstances, it is best not to start up and start up the mobile phone software. Starting up and starting up is generally applicable to the terminal of android system, and the corresponding project is run when starting up. This project is the only one on this machine, and the code is below.

Create a new class AutoStartBroadcastReceiver integrated from BroadcastReceiver:


public class AutoStartBroadcastReceiver extends BroadcastReceiver {

 private static final String ACTION = "android.intent.action.BOOT_COMPLETED";

 @Override
 public void onReceive(Context context, Intent intent) {
  // Startup startup 
  if (ACTION.equals(intent.getAction())) {
   Intent mainIntent = new Intent(context, MainActivity.class);//MainActivity.calss For the interface you want to open, if there is no interface, you can use it service Substitute 
   //  In BroadcastReceiver Display in Activity You must set the FLAG_ACTIVITY_NEW_TASK Sign 
   mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   context.startActivity(mainIntent);
  }
 }
}

Set permissions and register broadcasts in the AndroidManifest. xml file:


<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

 <receiver android:name=".AutoStartBroadcastReceiver" >
   <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <category android:name="android.intent.category.HOME" />
   </intent-filter>
 </receiver>

In this way, the code part is complete and you can test it! I have tested both the android 7.0 simulator and the Android 6.0 real machine (Meizu) to achieve the expected functions.

Here are two commands to send the "BOOT_COMPLETED" system broadcast over adb:

1. adb shell am broadcast-a android. intent. action. BOOT_COMPLETED

adb sends system broadcasts to android devices

2. adb shell am broadcast-a android. intent. action. BOOT_COMPLETED-c android. intent. category. HOME-n component

adb sends the system broadcast to the component specified by the android device.-n is followed by the full path of the class that you specify the project needs to receive the broadcast, instead of packagename as most people say on the Internet, such as "com. android. qrcode/. AutoStartBroadcastReceiver" in my project.

The above two commands are also valid for pro-testing!


Related articles: