【讨论】通过bindService启动的service,在unbindService后service也结束了
这个问题以前讨论过,各个文档blog上也是这么写的,但有人还是说即使unbindService了,service还是会继续运行。
但刚才验证,unbindService后,执行了service的onDestroy方法,service也停止了。
下面是一个通过servcie来实现计数的功能,并且在activity中显示出此计数
1.接口ICountService.java,只有一个返回计数值的函数声明
package com.min.localservicedemo;public interface ICountService { public abstract int getCount();}
package com.min.localservicedemo;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class CountService extends Service implements ICountService { private static final String TAG = "CountService"; private boolean threadDisable; private int count; private ServiceBinder serviceBinder = new ServiceBinder(); public class ServiceBinder extends Binder implements ICountService { public int getCount() { // TODO Auto-generated method stub return count; } } public int getCount() { // TODO Auto-generated method stub return count; } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return serviceBinder; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); new Thread(new Runnable() { public void run() { // TODO Auto-generated method stub while(!threadDisable) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } count++; Log.d(TAG, "count is " + count); updateCount(); } } } ).start(); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub return super.onUnbind(intent); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); threadDisable = true; Log.d(TAG, "onDestroy"); } /** * Update view */ public void updateCount() { LocalServiceDemo.getMyHandler().sendEmptyMessage(1); }}
package com.min.localservicedemo;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.Handler;import android.os.IBinder;import android.os.Message;import android.util.Log;import android.widget.TextView;public class LocalServiceDemo extends Activity { private static final String TAG = "LocalServiceDemo"; private static TextView tvCount; private static ICountService countService; private static MyHandler myHandler = MyHandler.getInstance(); private ServiceConnection serviceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub countService = (ICountService)service; Log.d(TAG, "onServiceConnected count is " + countService.getCount()); tvCount.setText("Count:" + countService.getCount()); } public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub countService = null; } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvCount = (TextView)findViewById(R.id.tvCount); this.bindService(new Intent("com.min.localservicedemo.CountService"), serviceConnection, BIND_AUTO_CREATE); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); this.unbindService(serviceConnection); Log.d(TAG, "onDestroy unbindService"); } public static class MyHandler extends Handler { private static MyHandler myHandler = null; private MyHandler() { } /** * Single Instance * @return */ public static MyHandler getInstance() { if (myHandler == null) { myHandler = new MyHandler(); } return myHandler; } @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub if(msg.what == 1) { tvCount.setText("Count:" + countService.getCount()); } super.handleMessage(msg); } } public static MyHandler getMyHandler() { return myHandler; }}