android教程之service使用方法示例详解
本文导语: Service的生命周期 (适用于2.1及以上) 1. 被startService的无论是否有任何活动绑定到该Service,都在后台运行。onCreate(若需要) -> onStart(int id, Bundle args). 多次startService,则onStart调用多次,但不会创建多个Service实例,只需要一次sto...
Service的生命周期 (适用于2.1及以上)
1. 被startService的
无论是否有任何活动绑定到该Service,都在后台运行。onCreate(若需要) -> onStart(int id, Bundle args). 多次startService,则onStart调用多次,但不会创建多个Service实例,只需要一次stop。该Service一直后台运行,直到stopService或者自己的stopSelf()或者资源不足由平台结束。
2. 被bindService的
调用bindService绑定,连接建立服务一直运行。未被startService只是BindService,则onCreate()执行,onStart(int,Bundle)不被调用;这种情况下绑定被解除,平台就可以清除该Service(连接销毁后,会导致解除,解除后就会销毁)。
3. 被启动又被绑定
类似startService的生命周期,onCreate onStart都会调用。
4. 停止服务时
stopService时显式onDestroy()。或不再有绑定(没有启动时)时隐式调用。有bind情况下stopService()不起作用。
以下是一个简单的实现例子,某些部分需要配合logcat观察。
AcMain.java
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 when the activity is first created. */
@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
MyService.java
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(); // 可以不用
}
}
AnndroidManifest.xml