Preface:
Sometimes apps need to run automatically when they're powered on, such as a back-office service that automatically updates content from the Web. How to realize the application of automatic running on the boot? At the time of writing, Lenovo to Mr. Gao to "Don t call me, I'll call you back!" Summarizing the Android framework, it's really a point. Understanding the meaning of this sentence, many of the Android platform to achieve a certain function of the problem, can be solved.
Usage Scenarios:
After the phone is powered on, the program runs automatically.
{Just recently I do Wanda star Order system, need to use the system to start the app directly to avoid the waiter to play other apps to study, the following share is my reference network, personally try to success code, posted out with you to share, hope to help more people! Don't know can ask me oh. qq:11745405 }
Background Knowledge:
When Android starts, it emits a system broadcast with the contents of action_boot_completed, whose string constants are represented as Android.intent.action.BOOT_COMPLETED. Just "snap" to this message in the program and start it again. Remember, the Android framework says, "Don ' t call me, I'll be back." What we have to do is wait until the message is received, and the means to achieve it is to achieve a broadcastreceiver.
Code parsing:1, interface mainactivity. java
Package com.example;Import Android.app.Alertdialog;Import Android.content.Dialoginterface;Import Android.os.Bundle;Import android.view.*;Import Android.widget.Toast;publicClass Mainactivity Extends Activity{Receive a message to start this activity @Override public void OnCreate (bundle savedinstancestate) {super.onCreate ( Savedinstancestate); super.setintegerproperty ( "SplashScreen", r.drawable.wandapad_splash) ; textview TV = new textview ( this) tv.settext (" Hello this is mainactivity "); Setcontentview (TV); }
2. Receive broadcast message: Bootbroadcastreceiver.java
Package com.example;Import Android.content.Broadcastreceiver;Import Android.content.Context;Import Android.content.Intent;/** * Created with IntelliJ idea. * User: Jia Goyang * date:13-6-5 * Time: PM 8:25 * To change this template with File | Settings | File Templates. */publicClass Bootbroadcastreceiver Extends Broadcastreceiver{StaticFinalStringACTION ="Android.intent.action.BOOT_COMPLETED";@Override public void OnReceive (Context Context,intent Intent) { if (Intent.getaction (). Equals (ACTION ) {intent mainactivityintent = new intent (context, mainactivity. class); //to start activity mainactivityintent.addflags (intent. flag_activity_new_task); Context.startactivity (mainactivityintent); } }}
The class derives from the Broadcastreceiver, and in the method OnReceive, detects whether the received intent conforms to the boot_completed and, if so, initiates mainactivity.
3. Configuration file: Androidmanifest.xml
Add the configuration within application:
<Receiver Android:name=". Bootbroadcastreceiver "><intent-filter> <action android:name= "Android.intent.action.BOOT_COMPLETED" Span class= "Hljs-tag" >/> </intent-filter> </receiver>
A receiver is registered to the system, and the child node Intent-filter indicates receiving the Android.intent.action.BOOT_COMPLETED message.
4. Add the following permissions in manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
5, Done! Extended thinking:
In most cases, a service that runs in the background is not a program that has an interface to run automatically. At this point, it is necessary to use StartService to start the corresponding service.
Android Boot Autorun app--broadcastreceiver