This is a very simple process. Unlike a PC, it requires a precise memory address. The so-called simplicity here is compared with that of a PC. The purpose of the Android system is not to come to me. I will come to you.
Activity seems to be the eyes of an application. The user uses his fingers to tell the activity what I want. Broadcastreceiver is like an android program that accepts intents from various parties. The service is like the Android app's hand. It completes the received messages correctly. Finally, I am going to group them into headers. Although I only have a deep understanding of the sentence activity is the eyes, the remaining two sentences are not as brilliant as the first one, but the intuition is very incisive, So I copied them from the book.
The process is like this. When all Android system services are started, messages will be distributed out like leaflets. This process is broadcast, what we need to do is to capture the message completed by the system startup. After capturing the message, the activity will be started and the service will be started. The best way is to practice.
You can configure androidmanifest in three places. XML. First, the custom classes inherited from broadcast must be registered in the XML file. Second, the custom classes inherited from the service must be registered in the XML file. Third, capture broadcast messages started by a system. Start code pasting
This is the class that receives broadcasts,
- Package opq. broadcast;
- Import Android. content. broadcastreceiver;
- Import Android. content. context;
- Import Android. content. intent;
- Import Android. util. log;
- Public class bootbroadcastreceiver extends broadcastreceiver {
- Public static final string action = "android. Intent. Action. boot_completed ";
- @ Override
- Public void onreceive (context, intent ){
- // Todo auto-generated method stub
- If (intent. getaction (). Equals (Action )){
- Log. D ("tag", "OK ");
- Intent myintent = new intent (); // The intent object is used to start the service.
- Myintent. setaction ("opq. Service. bootservice ");
- Context. startservice (myintent); // start the service
- }
- }
- }
Copy code
This is a service class started after receiving the broadcast. It inherits from the service and must implement the onbind (intent) method. oncreate is called only once when the service is started for the first time, the onstart method will be called later unless the service is restarted.
- Package opq. Service;
- Import opq. boot. R;
- Import Android. App. Service;
- Import Android. content. intent;
- Import Android. Media. mediaplayer;
- Import Android. OS. ibinder;
- Import Android. provider. mediastore. Audio. Media;
- Import Android. util. log;
- Public class bootservice extends Service {
- Mediaplayer;
- @ Override
- Public ibinder onbind (intent ){
- // Todo auto-generated method stub
- Return NULL;
- }
- @ Override
- Public void oncreate (){
- // Todo auto-generated method stub
- Log. D ("tag", "bootservice oncreate ");
- Mediaplayer = mediaplayer. Create (getapplicationcontext (), R. Raw. anhao );
- Super. oncreate ();
- }
- @ Override
- Public void onstart (intent, int startid ){
- // Todo auto-generated method stub
- Log. D ("tag", "bootservice onstart ");
- Mediaplayer. Start ();
- Mediaplayer. setlooping (false );
- Super. onstart (intent, startid );
- }
- }
Copy code
The whole program has only two classes if you want, and the activity is redundant. In this example, the R. Raw. anhao; raw is a new folder under the res directory.
Finally, paste the XML file: in the XML file, I missed the class name configuration and found it for a long time. This tool is difficult to test and restarts the simulator every time.
Java code:
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
- Package = "opq. Boot"
- Android: versioncode = "1"
- Android: versionname = "1.0" type = "codeph" text = "/codeph">
- <Uses-SDK Android: minsdkversion = "7"/>
- <Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
- <Activity Android: Name = ". bootmusicactivity"
- Android: Label = "@ string/app_name">
- <Intent-filter>
- <Action Android: Name = "android. Intent. Action. Main"/>
- <Category Android: Name = "android. Intent. Category. launcher"/>
- </Intent-filter>
- </Activity>
- <Cycler Android: Name = "opq. Broadcast. bootbroadcastreceiver">
- <Intent-filter>
- <Action Android: Name = "android. Intent. Action. boot_completed"/>
- </Intent-filter>
- </Cycler>
- <Service android: Name = "opq. Service. bootservice">
- <Intent-filter>
- <Action Android: Name = "opq. Service. bootservice"/>
- </Intent-filter>
- </Service>
- </Application>
- <Uses-Permission Android: Name = "android. Permission. receive_boot_completed"/>
- </Manifest>