In the project to use the power-on Autorun function, so learn the next, here as notes recorded.
The main is the following 4 a step:
1 , the principle of understanding:
search data found that when Android starts, a system broadcast is issued, the content is action_boot_completed, its string constants are expressed as Android.intent.action.BOOT_COMPLETED, So we just need to receive this broadcast in our own app and then launch the app .
2 , writing the receiver
since it is receiving broadcasts, it is necessary to use a broadcast receiver, so create a new one that inherits from the broadcast Broadcastreceiver class to receive the above system broadcasts specifically.
Importandroid.content.context;importandroid.content.intent; Broadcast received, boot from 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, judging is the broadcast after the boot, jump to their own applications. Here is the beginning of the loginactivity.
3 , registered broadcast
in the manifest.xml when the system sends a start-up broadcast, it will enter Com.baby.activity.BootBroadcastReceiver in this class, we have just written the receiver class.
<application android:allowbackup= "true" android:hardwareaccelerated= "false" android:icon= "@ Drawable/ic_launcher " android:label=" @string/app_name " android:theme=" @style/apptheme "> < Receiverandroid:name= "Com.baby.activity.BootBroadcastReceiver" > <intent-filter> < Actionandroid:name= "Android.intent.action.BOOT_COMPLETED"/> <categoryandroid:name= " Android.intent.category.HOME "/> </intent-filter> </receiver> </application>
4 , claim permissions
When it comes to booting up, it is necessary to give this application the appropriate permissions. So Add Permissions in the Manifest.xml
<uses-permissionandroid:name= "Android.permission.RECEIVE_BOOT_COMPLETED" > </uses-permission>