Android notes boot from boot

Source: Internet
Author: User

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>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.