[Android instance] enable service on Android

Source: Internet
Author: User

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,

  1. Package opq. broadcast;
  2. Import Android. content. broadcastreceiver;
  3. Import Android. content. context;
  4. Import Android. content. intent;
  5. Import Android. util. log;
  6. Public class bootbroadcastreceiver extends broadcastreceiver {
  7. Public static final string action = "android. Intent. Action. boot_completed ";
  8. @ Override
  9. Public void onreceive (context, intent ){
  10. // Todo auto-generated method stub
  11. If (intent. getaction (). Equals (Action )){
  12. Log. D ("tag", "OK ");
  13. Intent myintent = new intent (); // The intent object is used to start the service.
  14. Myintent. setaction ("opq. Service. bootservice ");
  15. Context. startservice (myintent); // start the service
  16. }
  17. }
  18. }

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.

  1. Package opq. Service;
  2. Import opq. boot. R;
  3. Import Android. App. Service;
  4. Import Android. content. intent;
  5. Import Android. Media. mediaplayer;
  6. Import Android. OS. ibinder;
  7. Import Android. provider. mediastore. Audio. Media;
  8. Import Android. util. log;
  9. Public class bootservice extends Service {
  10. Mediaplayer;
  11. @ Override
  12. Public ibinder onbind (intent ){
  13. // Todo auto-generated method stub
  14. Return NULL;
  15. }
  16. @ Override
  17. Public void oncreate (){
  18. // Todo auto-generated method stub
  19. Log. D ("tag", "bootservice oncreate ");
  20. Mediaplayer = mediaplayer. Create (getapplicationcontext (), R. Raw. anhao );
  21. Super. oncreate ();
  22. }
  23. @ Override
  24. Public void onstart (intent, int startid ){
  25. // Todo auto-generated method stub
  26. Log. D ("tag", "bootservice onstart ");
  27. Mediaplayer. Start ();
  28. Mediaplayer. setlooping (false );
  29. Super. onstart (intent, startid );
  30. }
  31. }

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:

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Package = "opq. Boot"
  4. Android: versioncode = "1"
  5. Android: versionname = "1.0" type = "codeph" text = "/codeph">
  6. <Uses-SDK Android: minsdkversion = "7"/>
  7. <Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
  8. <Activity Android: Name = ". bootmusicactivity"
  9. Android: Label = "@ string/app_name">
  10. <Intent-filter>
  11. <Action Android: Name = "android. Intent. Action. Main"/>
  12. <Category Android: Name = "android. Intent. Category. launcher"/>
  13. </Intent-filter>
  14. </Activity>
  15. <Cycler Android: Name = "opq. Broadcast. bootbroadcastreceiver">
  16. <Intent-filter>
  17. <Action Android: Name = "android. Intent. Action. boot_completed"/>
  18. </Intent-filter>
  19. </Cycler>
  20. <Service android: Name = "opq. Service. bootservice">
  21. <Intent-filter>
  22. <Action Android: Name = "opq. Service. bootservice"/>
  23. </Intent-filter>
  24. </Service>
  25. </Application>
  26. <Uses-Permission Android: Name = "android. Permission. receive_boot_completed"/>
  27. </Manifest>
Related Article

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.