Android Boot launcher

Source: Internet
Author: User

Android program implementation of the principle of start-up, the simple point is to do a broadcast receiver, receive the power on the radio to start activity or service or perform other operations. Android system will be launched when the launch of a broadcast, the content is action_boot_completed, we write a receiver, each time we receive this broadcast, it is just boot, and then start the need to start the program on the line. Concrete steps: First step, new Bootbroadcastreciever.java, Inherit broadcastreceiver,

public class Bootbroadcast extends broadcastreceiver { @Override public void onreceive (context context, Intent Intent) {         / * If the fruit is received into the open broadcast * * if (intent. Getaction (). equals (Intent. action_boot_completed)) {          // this line open machine Kai dynamic , than such as Kai move Activity or service         }}} If you are starting an activity, if you want to start mainactivity, the IF statement block reads: Intent newintent = new Intent (context, mainactivity.class);  Newintent.addflags (Intent.flag_activity_new_task); This mark must be added context.startactivity (newintent); If you are starting a service, if you want to start Mainservice, the IF statement block reads: Context.startservice (new Intent (context, mainservice.class)); This sentence can also be split into two sentences note: Activity and service are to be registered in Manifest.xml
The second step is to register the above broadcast receiver in manifest Bootbroadcastreciever<receiver android:name=". Bootbroadcastreciever " > <intent-filter> <action android:name="Android.intent.action.BOOT_COMPLETED" /> /c6> <action android:name="Android.intent.action.DATA_STATE" /> /c14> <action android:name="Android.intent.action.PACKAGE_RESTARTED" /> </intent-filter> </receiver> Note: Other textbooks on the web are only written in the filter.<action android:name="Android.intent.action.BOOT_COMPLETED" /> This sentence, This statement affirms the right to receive a power-on broadcast. Some textbooks may be more <category android:name= "Android.intent.category.Home"/> This category tag, I do not know what this means, but seemingly and boot-start unrelated, It's no problem to delete it. As <action android:name="Android.intent.action.DATA_STATE" /> <action android:name="Android.intent.action.PACKAGE_RESTARTED" />
These two sentences, is guaranteed in 4.0 of the system to start the service is the user in the "Management application" inside to force stop, the next time the service can still start, if not these two sentences, The service will not start automatically the next time the user starts the service by forcing it to stop.
The third step is to register the permission to listen for the boot broadcast in manifest.xml manifest.xml add <uses-permission android:name= "android.permission.RECEIVE_BOOT_ Completed "/> This step is not mentioned in some tutorials, this seems to be the new version of the SDK to write this, the old version (usually referred to as API 111) does not need this step, specifically I do not know, anyway more this step more insurance.

Android Boot launcher

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.