深入理解Android的startservice和bindservice

来源:互联网 发布:如何推网络歌曲 编辑:程序博客网 时间:2024/05/29 21:30

一、Android设计services的目的
        1.service运行在后台,得以让我们运行在前台的时候去做一些其他的操作,如后台下载,播放音乐 2.Android系统安全的原因导致了我们在不同的进程间无法使用一般的方式共享数据,我们可以用services的aidl实现共享操作
二、startServices
      一旦被调用,如果没有程序停止它或者它自己停止,service将一直运行。在这种模式下,service开始于调用Context.startService() ,停止于Context.stopService(). service可以通过调用Android Service 生命周期() 或 Service.stopSelfResult()停止自己。不管调用多少次startService() ,只需要调用一次 stopService() 就可以停止servic。
       1.定义一个service
public class ServiceTest extends Service {
    public IBinder onBind(Intent intent) {
        return null;
    }
    public void onCreate() {
        super.onCreate();
    }


    public void onDestroy() {
        super.onDestroy();
    }


//此方法是为了可以在Context中获得服务的实例,bindServices中用到
class ServiceBinder extends Binder {
        public ServiceTest getService() {
            return ServiceTest.this;
        }
    }
}
    2.启动services
public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);      
  Intent intent = new Intent(MainActivity.this,ServiceTest.class);
        startService(intent);
    }

    };

    protected void onStop() {
        super.onStop();
Intent intent = new Intent(MainActivity.this,ServiceTest.class);
stopService(intent);
    }
}
三、bindServices
   客户端建立一个与Service的连接,并使用此连接与Service进行通话,通过Context.bindService()方法来绑定服务,Context.unbindService()方法来关闭服务。多个客户端可以绑定同一个服务,如果Service还未被启动,bindService()方法可以启动服务。启动流程:context.bindService()  ——> onCreate()  ——> onBind()  ——> Service running  ——> onUnbind()  ——> onDestroy()  ——> Service stop
    onBind()将返回给客户端一个IBind接口实例,IBind允许客户端回调服务的方法,比如得到Service的实例、运行状态或其他操作。这个时候把调用者(Context,例如Activity)会和Service绑定在一起,Context退出了,Srevice就会调用onUnbind->onDestroy相应退出。
所以调用bindService的生命周期为:onCreate --> onBind(只一次,不可多次绑定) --> onUnbind --> onDestory。


如果一个service允许别人绑定,那么需要实现以下额外的方法:
      IBinder onBind(Intent intent)
      boolean onUnbind(Intent intent)
      void onRebind(Intent intent)
onBind()回调方法会继续传递通过bindService()传递来的intent对象,onUnbind()会处理传递给unbindService()的intent对象。如果service允许绑定,onBind()会返回客户端与服务互相联系的通信句柄(实例)。如果建立了一个新的客户端与服务的连接,onUnbind()方法可以请求调用onRebind()方法。
记住: 任何服务无论它怎样建立,默认客户端都可以连接,所以任何service都能够接收onBind()和onUnbind()方法。


public class BindActivity extends Activity {


    ServicesTest testServices;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);    
        Intent intent = new Intent(BindActivity.this, TestService.class);
        /** 进入Activity开始服务 */
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }
    private ServiceConnection conn = new ServiceConnection() {
        /** 获取服务对象时的操作 */
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            testService = ((ServiceTest.ServiceBinder) service).getService();
        }
        /** 无法获取到服务对象时的操作 */
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
            testService = null;
        }
    };
    protected void onDestroy() {
        super.onDestroy();
        this.unbindService(conn);
    }
}


至于startservice和bindservice的使用场景,有网友这么说:


1.通过startservice开启的服务.一旦服务开启, 这个服务和开启他的调用者之间就没有任何的关系了. 调用者不可以访问 service里面的方法. 调用者如果被系统回收了或者调用了ondestroy方法, service还会继续存在。  
2.通过bindService开启的服务,服务开启之后,调用者和服务之间 还存在着联系 , 一旦调用者挂掉了.service也会跟着挂掉。注意:bindServices一定要调用unbindServices方法,否则会抛出一个serviceConnection泄露异常

0 0
原创粉丝点击