android Service的用法
Service是后台运行的服务,启动Service可以StartService或者bindservice
看看Service的生命周期。
当调用StartService时,先走oncreat方法,在走onstart方法。当stopservice时,走ondestory方法。
可以绑定一个Service,让它在后台执行,比方说后台播放歌曲等就是这样的。
当绑定一个Service时,用bindService方法,这时先走oncreate方法,在走onbind方法。当想停止Service时,用onunbindService方法,这时先走onUnbind方法,在走ondestory方法。
上代码:
package com.android.test; import android.app.Service; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; public class TestService extends Service { @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub System.out.println("onBind"); return null; } @Override public void onCreate() { // TODO Auto-generated method stub System.out.println("onCreate"); super.onCreate(); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub System.out.println("onStart"); super.onStart(intent, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub System.out.println("onDestroy"); super.onDestroy(); } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub System.out.println("onUnbind"); return super.onUnbind(intent); } @Override public void onRebind(Intent intent) { // TODO Auto-generated method stub System.out.println("onRebind"); super.onRebind(intent); } }
上面这个是Service代码,下面这个是Activity的代码:
代码
package com.android.test;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TestActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("com.android.testservice");
switch(arg0.getId()) {
case R.id.button1:
// startService(intent);
bindService(intent, sc, BIND_AUTO_CREATE);
break;
case R.id.button2:
// stopService(intent);
unbindService(sc);
break;
}
}
ServiceConnection sc = new ServiceConnection() {
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
System.out.println("onServiceConnected");
}
public void onServiceDisconnected(ComponentName arg0) {
System.out.println("onServiceDisconnected");
}
};
}
哦,对了还要记得在manifest里面注册呢。
<service android:name=".TestService">
<intent-filter>
<action android:name="com.android.testservice"/>
</intent-filter>
</service>
当调用startService方法的时候,打印出来的log是:
oncreate
onstart
当调用stopService方法的时候,打印出来的log是:
ondestory
当调用bindService和unbindServide方法时,打印出来的log是:
代码
11-25 12:30:47.837: INFO/System.out(335): onCreate
11-25 12:30:47.887: INFO/System.out(335): onBind
11-25 12:30:49.838: INFO/System.out(335): onUnbind
11-25 12:30:49.872: INFO/System.out(335): onDestroy