android application realizes the automatic startup method

  • 2020-06-19 11:44:57
  • OfStack

How it works: The Android system emits a broadcast when it starts up. So we can receive the broadcast and launch our application. The broadcast receiver must be configured in xml because the broadcast receiver configured in xml does not exit with the application.

Broadcast receiver:


package com.yangshidesign.boot;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
 Intent i = new Intent(context, UnityPlayerNativeActivity.class);
 // This has to be added flags
 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 context.startActivity(i);
 }
}

Configure in the application tag of manifest:


  <!--  Powered up  -->
 <receiver android:name="com.yangshidesign.boot.BootReceiver">
 <intent-filter>
  <action android:name="android.intent.action.BOOT_COMPLETED"/>
  <category android:name="android.intent.category.HOME"/>
 </intent-filter>
 </receiver>

Add permissions:


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

That will do.
I used the Red Rice note test, which has to be set one time:
Click Settings app to find your app. Click and drag down the right Management app to start automatically.


Related articles: