Method for starting android and self starting apk

  • 2021-10-13 08:29:26
  • OfStack

Android boot self-startup is realized by BroadcastReceiver registration boot broadcast

Android receives the boot broadcast, which requires the broadcast receiver BroadcastReceiver component.

Specific code:

1. Register receiver with the system in the configuration file AndroidManifest. xml


<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>

2. Need to add appropriate permissions


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

3. Create a broadcast class, and you can add the operation needed for boot in Receiver


public class BootCompletedReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    //  Code executed after boot 
    Intent intent2 = new Intent(context, SplashActivity.class);
  intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startActivity(intent2);

 }
}

This is the flashscreen page of the boot startup program, where

intent2.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK); Must

The key point is: the condition of starting up and starting up is that after installing APK, you need to manually click on the software once, and then start up again before starting up.

Otherwise, it will not start itself, because you need to register for broadcasting. . .


Related articles: