The Automatic startup function is used in the project, so I learned how to record it as a note.
The procedure is as follows:
1, Principles:
After searching the data, it is found that when Android is started, a system broadcast is sent with the content ACTION_BOOT_COMPLETED, and its String constant is represented as android. intent. action. BOOT_COMPLETED, so we only need to receive this broadcast in our own application, and then start the APP.
2, Write the receiver
Since broadcast receiver is used to receive broadcast, a new class inherited from BroadcastReceiver is created to receive the above system broadcast.
Importandroid. content. context; importandroid. content. intent; // broadcast receiver. public classBootBroadcastReceiver extends BroadcastReceiver {static final Stringaction_boot = "android. intent. action. BOOT_COMPLETED "; @ Override public void onReceive (Context context, Intent intent) {if (intent. getAction (). equals (action_boot) {Intent autoIntent = newIntent (context, LoginActivity. class); autoIntent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); context. startActivity (autoIntent );}}}
The above code is to receive the broadcast. After judging that the broadcast is started, it will jump to its own application. Here is the starting LoginActivity.
3, Register Broadcast
Register the broadcast in manifest. xml. When the system sends a boot broadcast, it enters the com. baby. activity. BootBroadcastReceiver class, which is the receiver class we just compiled.
4, Declare Permissions
When it comes to boot, you must grant the corresponding permissions to the application. Therefore, add permissions to manifest. xml.