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
- Package com.android.myservice;
- Import android.app.Activity;
- Import android.content.Intent;
- Import Android.os.Bundle;
- Import Android.util.Log;
- Import Android.view.View;
- Import Android.view.View.OnClickListener;
- Import Android.widget.Button;
- Public class Servicedemo extends Activity implements Onclicklistener {
- private static final String TAG = "Servicedemo";
- Button Buttonstart, Buttonstop;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Buttonstart = (Button) Findviewbyid (R.id.buttonstart);
- Buttonstop = (Button) Findviewbyid (r.id.buttonstop);
- Buttonstart.setonclicklistener (this);
- Buttonstop.setonclicklistener (this);
- }
- public void OnClick (View src) {
- switch (Src.getid ()) {
- Case R.id.buttonstart:
- LOG.I (TAG, "onclick:starting service");
- StartService (new Intent (This, MyService. Class));
- Break ;
- Case R.id.buttonstop:
- LOG.I (TAG, "onclick:stopping service");
- StopService (new Intent (This, MyService. Class));
- Break ;
- }
- }
- }
In addition to the manifest inside the statement service: (Androidmanifest.xml)
[HTML]View PlainCopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="Com.android.myservice">
- <application android:label="@string/app_name">
- <activity android:name=". Servicedemo " android:label=" @string/app_name ">
- <intent-filter>
- <action android:name="Android.intent.action.MAIN"/>
- <category android:name="Android.intent.category.LAUNCHER"/>
- </intent-filter>
- </Activity>
- <service android:enabled="true" android:name= ". MyService "/>
- </Application>
- </manifest>
Define Service (Myservice.java)
[Java]View PlainCopy
- Package com.android.myservice;
- Import Android.app.Service;
- Import android.content.Intent;
- Import Android.media.MediaPlayer;
- Import Android.os.IBinder;
- Import Android.util.Log;
- Import Android.widget.Toast;
- Public class MyService extends Service {
- private static final String TAG = "MyService";
- MediaPlayer player;
- @Override
- Public IBinder Onbind (Intent Intent) {
- return null;
- }
- @Override
- public void OnCreate () {
- Toast.maketext (This, "My Service created", Toast.length_long). Show ();
- LOG.I (TAG, "onCreate");
- Player = Mediaplayer.create (this, R.raw.braincandy);
- Player.setlooping (false);
- }
- @Override
- public void OnDestroy () {
- Toast.maketext (This, "My Service stoped", Toast.length_long). Show ();
- LOG.I (TAG, "OnDestroy");
- Player.stop ();
- }
- @Override
- public void OnStart (Intent Intent, int startid) {
- Toast.maketext (This, "My Service Start", Toast.length_long). Show ();
- LOG.I (TAG, "OnStart");
- Player.start ();
- }
- }
The Layout folder is Main.xml
[HTML]View PlainCopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height= "wrap_content" android:text= "@string/services_demo" android:gravity= "center" android:textsize= "20SP" android:padding= "20DP" />
- <button android:layout_width=< Span class= "Attribute-value" > "wrap_content" android:layout_height=" wrap_content " android:id=" @+id/ Buttonstart " android:text=" @string/start " ></button>
- <button android:layout_width= "wrap_content" android:layout_height="Wrap_content " android:text="@string/stop" android:id="@+id/buttonstop"></Button>
- </linearlayout>
The Values folder is Strings.xml
[HTML]View PlainCopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <resources>
- <string name="Start">start</string>
- <string name="Stop">stop</string>
- <string name="Services_demo">service demo</string>
- </Resources>
Android Boot Background Runtime program (Service)