Android boot is simple, and android boot is simple.
Today, we will mainly discuss how android can enable a service to automatically start upon startup. The Android mobile phone will trigger a Standard Broadcast Action during startup, called android. intent. action. BOOT_COMPLETED (remember to trigger only once). Here we can build a broadcast receiver to receive this action. below is a simple example of the following implementation steps:
Step 1: Create a broadcast receiver and reconstruct its abstract method onReceive (Context context, Intent intent) to start the Service or app you want to start.
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. util. Log;
Public class BootBroadcastReceiver extends BroadcastReceiver {
// Override the onReceive Method
@ Override public void onReceive (Context context, Intent intent ){
// The XXX. class at the end is the service to be started.
Intent service = new Intent (context, XXXclass );
Context. startService (service );
Log. v ("TAG", "automatic Service Startup .....");
// Start the application. The parameter is the package name of the application to be automatically started.
Intent intent2 = context. getPackageManager (). getLaunchIntentForPackage (packageName );
Context. startActivity (intent2 );
}
}
Step 2: configure the xml file and add the intent-filter configuration in the receiver.
<Cycler android: name = "BootBroadcastReceiver">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED"> </action>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Cycler>
Step 3: add permissions <uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"/>
In this way, you can start the system on your own, but there is a problem that sometimes it may take several seconds to enable a certain latency. It may be that the system did not respond to the problem and the broadcast was not sent in time...