Android Tutorial Service Usage examples detailed _android

Source: Internet
Author: User

Service Lifecycle (applies to 2.1 and above)

1. The StartService
Regardless of whether any activity is bound to the service, it runs in the background.  OnCreate (if required)-> onStart (int id, Bundle args). Multiple StartService, the onstart is called multiple times, but no multiple service instances are created, and only one stop is required. The service runs in the background until StopService or its own stopself () or insufficient resources are completed by the platform.

2. The Bindservice
Call Bindservice binding, the connection Establishment service has been running. is not startservice just bindservice, then OnCreate () execution, OnStart (Int,bundle) is not called, in which case the binding is lifted, the platform can clear the service (after the connection is destroyed, will cause the release, Will be destroyed after it is lifted).

3. Be activated and bound
Similar to the StartService life cycle, OnCreate OnStart will call.

4. When the service is discontinued
Explicit OnDestroy () when StopService. Implicitly called when there is no longer a binding (when it is not started). StopService () does not work with bind conditions.

Here is a simple implementation example where some parts need to be logcat observed.
Acmain.java

Copy Code code as follows:

Package jtapp.myservicesamples;
Import android.app.Activity;
Import Android.content.ComponentName;
Import Android.content.Context;
Import android.content.Intent;
Import android.content.ServiceConnection;
Import Android.os.Bundle;
Import Android.os.IBinder;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Acmain extends activity implements Onclicklistener {
private static final String TAG = "Acmain";
Private Button btnstart;
Private Button btnstop;
Private Button Btnbind;
Private Button Btnexit;

/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Findview ();
}
private void Findview () {
btnstart = (Button) Findviewbyid (R.id.start);
Btnstop = (Button) Findviewbyid (r.id.stop);
Btnbind = (Button) Findviewbyid (R.id.bind);
Btnexit = (Button) Findviewbyid (r.id.exit);

Btnstart.setonclicklistener (this);
Btnstop.setonclicklistener (this);
Btnbind.setonclicklistener (this);
Btnexit.setonclicklistener (this);
}
@Override
public void OnClick (View v) {
Intent Intent = new Intent ("Jtapp.myservicesamples.myservice");
Switch (V.getid ()) {
Case R.id.start:
StartService (Intent);
Toast.maketext (This,
"MyService Running" + myservice.msec/1000.0 + "s.",
Toast.length_long). Show ();
Break
Case R.id.stop:
StopService (Intent);
Toast.maketext (This,
"MyService Running" + myservice.msec/1000.0 + "s.",
Toast.length_long). Show ();
Break
Case R.id.bind:
Bindservice (Intent, SC, context.bind_auto_create);
Break
Case R.id.exit:
This.finish ();
Break
}
}

Private MyService Servicebinder;

Private Serviceconnection sc = new Serviceconnection () {
@Override
public void onservicedisconnected (componentname name) {
LOG.D (TAG, "in onservicedisconnected");
Servicebinder = null;
}
@Override
public void onserviceconnected (componentname name, IBinder service) {
LOG.D (TAG, "in onserviceconnected");
Servicebinder = ((myservice.mybinder) service). GetService ();
}
};
@Override
protected void OnDestroy () {
This.unbindservice (SC);
This.stopservice (
New Intent ("Jtapp.myservicesamples.myservice"));
Super.ondestroy ();
}
}

Main.xml

Copy Code code as follows:

<?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" >
<textview android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" android:text= "@string/hello"/>
<button android:text= "Start myservice" android:id= "@+id/start"
Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/>
<button android:text= "Stop myservice" android:id= "@+id/stop"
Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/>
<button android:text= "Bind myservice" android:id= "@+id/bind"
Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/>
<button android:text= "Exit acmain" android:id= "@+id/exit"
Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/>
</LinearLayout>

Myservice.java

Copy Code code as follows:

Package jtapp.myservicesamples;
Import Android.app.Service;
Import android.content.Intent;
Import Android.os.Binder;
Import Android.os.IBinder;
Import Android.util.Log;
public class MyService extends Service {
private static final String TAG = "MyService";
public static Long msec = 0;
Private Boolean bthreadrunning = true;


Private final IBinder binder = new Mybinder ();
public class Mybinder extends Binder {
MyService GetService () {
return myservice.this;
}
}
@Override
Public IBinder Onbind (Intent Intent) {
return binder;
}
@Override
public void OnCreate () {
New Thread (New Runnable () {
@Override
public void Run () {
while (bthreadrunning) {
try {
Thread.Sleep (100);
catch (Interruptedexception e) {
}
LOG.I (TAG, "MyService running" + (MSEC+=100) + "Ms.");
}
}
). Start ();
}
@Override
public void OnDestroy () {
Bthreadrunning = false;
Super.ondestroy (); Can not
}
}

Anndroidmanifest.xml

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Jtapp.myservicesamples" android:versioncode= "1"
Android:versionname= "1.0" >
<application android:icon= "@drawable/icon" android:label= "@string/app_name"
Android:debuggable= "true" >
<activity android:name= ". Acmain "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:name= "MyService" >
<intent-filter>
<action android:name= "Jtapp.myservicesamples.myservice" ></action>
</intent-filter>
</service>
</application>
</manifest>

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.