Android Boot Background Runtime program (Service)

Source: Internet
Author: User

In Android development, use the service when you need to create a program that runs in the background. Service can be divided into two kinds: infinite life and limited life. It is important to note that the service is different from the activities (it can be understood as the difference between the background and the foreground), for example, if you need to use a service, you need to call StartService () to take advantage of StartService () Go to invoke the OnCreate () and OnStart () methods in the service to start a background service.
The process for starting a Service is as follows: Context.startservice ()->oncreate ()->onstart ()->service running where onCreate () You can do some initialization of the service, and OnStart () starts the service.
The process of stopping a service is as follows: Context.stopservice () | ->ondestroy ()->service stop
The next example is a small example of using a background service to play music, click Start to run the service, and click Stop Service.
Servicesdemo.java (is an activity)

[Java]View PlainCopy
  1. Package com.android.myservice;
  2. Import android.app.Activity;
  3. Import android.content.Intent;
  4. Import Android.os.Bundle;
  5. Import Android.util.Log;
  6. Import Android.view.View;
  7. Import Android.view.View.OnClickListener;
  8. Import Android.widget.Button;
  9. Public class Servicedemo extends Activity implements Onclicklistener {
  10. private static final String TAG = "Servicedemo";
  11. Button Buttonstart, Buttonstop;
  12. @Override
  13. public void OnCreate (Bundle savedinstancestate) {
  14. super.oncreate (savedinstancestate);
  15. Setcontentview (R.layout.main);
  16. Buttonstart = (Button) Findviewbyid (R.id.buttonstart);
  17. Buttonstop = (Button) Findviewbyid (r.id.buttonstop);
  18. Buttonstart.setonclicklistener (this);
  19. Buttonstop.setonclicklistener (this);
  20. }
  21. public void OnClick (View src) {
  22. switch (Src.getid ()) {
  23. Case R.id.buttonstart:
  24. LOG.I (TAG, "onclick:starting service");
  25. StartService (new Intent (This, MyService.   Class));
  26. Break ;
  27. Case R.id.buttonstop:
  28. LOG.I (TAG, "onclick:stopping service");
  29. StopService (new Intent (This, MyService.   Class));
  30. Break ;
  31. }
  32. }
  33. }




In addition to the manifest inside the statement service: (Androidmanifest.xml)

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="Com.android.myservice">
  4. <application android:label="@string/app_name">
  5. <activity android:name=". Servicedemo " android:label=" @string/app_name ">
  6. <intent-filter>
  7. <action android:name="Android.intent.action.MAIN"/>
  8. <category android:name="Android.intent.category.LAUNCHER"/>
  9. </intent-filter>
  10. </Activity>
  11. <service android:enabled="true" android:name= ". MyService "/>
  12. </Application>
  13. </manifest>




Define Service (Myservice.java)

[Java]View PlainCopy
  1. Package com.android.myservice;
  2. Import Android.app.Service;
  3. Import android.content.Intent;
  4. Import Android.media.MediaPlayer;
  5. Import Android.os.IBinder;
  6. Import Android.util.Log;
  7. Import Android.widget.Toast;
  8. Public class MyService extends Service {
  9. private static final String TAG = "MyService";
  10. MediaPlayer player;
  11. @Override
  12. Public IBinder Onbind (Intent Intent) {
  13. return null;
  14. }
  15. @Override
  16. public void OnCreate () {
  17. Toast.maketext (This, "My Service created", Toast.length_long). Show ();
  18. LOG.I (TAG, "onCreate");
  19. Player = Mediaplayer.create (this, R.raw.braincandy);
  20. Player.setlooping (false);
  21. }
  22. @Override
  23. public void OnDestroy () {
  24. Toast.maketext (This, "My Service stoped", Toast.length_long). Show ();
  25. LOG.I (TAG, "OnDestroy");
  26. Player.stop ();
  27. }
  28. @Override
  29. public void OnStart (Intent Intent, int startid) {
  30. Toast.maketext (This, "My Service Start", Toast.length_long). Show ();
  31. LOG.I (TAG, "OnStart");
  32. Player.start ();
  33. }
  34. }



The Layout folder is Main.xml

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:gravity="center">
  7. <TextView
  8. android:layout_width="fill_parent"
  9.     android:layout_height= "wrap_content"  android:text= "@string/services_demo"  android:gravity= "center"  android:textsize= "20SP"  android:padding= "20DP" />   
  10. <button android:layout_width=< Span class= "Attribute-value" > "wrap_content"  android:layout_height=" wrap_content " android:id=" @+id/ Buttonstart " android:text=" @string/start " ></button>  
  11. <button android:layout_width= "wrap_content" android:layout_height="Wrap_content "  android:text="@string/stop" android:id="@+id/buttonstop"></Button>
  12. </linearlayout>


The Values folder is Strings.xml

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <resources>
  3. <string name="Start">start</string>
  4. <string name="Stop">stop</string>
  5. <string name="Services_demo">service demo</string>
  6. </Resources>

Android Boot Background Runtime program (Service)

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.