Service组件简介

  Service是一个应用程序组件,没有图形化界面,通常用来处理一些耗时较长的操作,可以用Service更新ContentProvider,发送Intent以及启动系统的通知等等。Service并不是一个单独的进程,也不是一个线程。

  绑定Service和启动Service的区别:

      一个Activity启动一个Service之后不能去控制Service的过程和结果,但是实现简单,实现代码如下:

    首先声明一个名为FirstService的类,此类继承自Service类,然后复写Service类的onCreate()、onStartCommand()、onDestroy()方法,如果是首次启动该Service则会调用onCreate()方法和onStartCommand()方法,否则只调用onStartCommand()方法,如果停止该Service则会调用该Service的onDestroy()方法。

  ServiceActivity中启动Service的代码如下:

    Intent intent = new Intent();
    intent.setClass(ServiceActivity.this, FirstService.class);
    startService(intent);

  ServiceActivity中停止Service的代码如下:

    Intent intent = new Intent();
    intent.setClass(ServiceActivity.this, FirstService.class);
    stopService(intent);

      绑定Service提供了一种客户端和服务器的模式,通过这种模式Activity可以向Service发送请求,Service可以向Activity返回响应。

  首先在ServiceActivity中声明一个ServiceConnection类的对象conn用来绑定Service和Activity,然后在FirstActivity中声明一个MyBinder类继承自Binder类,复写Binder类的onTransact()方法,当ServiceActivity中获得的FirstService类返回的binder对象执行transact(code,data,reply,flags)方法时。同时执行FirstService类中MyBinder类的onTransact()方法。

  声明ServiceConnection类的对象conn:

    ServiceConnection conn = new ServiceConnection() {
      @Override
      public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub
      }
      @Override
      public void onServiceConnected(ComponentName name, IBinder service) {
        // TODO Auto-generated method stub
        //当绑定成功时,将SecondService返回的binder对象赋值给ServiceActivity的binder对象
        ServiceActivity.this.binder = (Binder)service;
      }
    };

  绑定Service详细代码如下:

    Intent intent = new Intent();
    intent.setClass(ServiceActivity.this, FirstService.class);
    bindService(intent, conn, BIND_AUTO_CREATE);

    ServiceActivity向FirstService发送响应请求和FirstService响应ServiceActivity的请求:

    //生成Parcel对象,Parcel对象就像一个数据集,可以传载数据
    Parcel data = Parcel.obtain();
    data.writeString("from Activity data");
    //此时声明的Parcel对象reply中的数据为空
    Parcel reply = Parcel.obtain();
    try {
      //data中是数据,当执行此方法执行时,同时会执行FirstService中的MyBinder对象的onTransact()方法
      binder.transact(0, data, reply, 0);
      String str = reply.readString();
      Log.d("MyDebug", str);
    } catch (RemoteException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  FirstService类中MyBinder对象的声明如下,需要复写该类的onTransact()方法:

    class MyBinder extends Binder{

    @Override
    protected boolean onTransact(int code, Parcel data, Parcel reply,int flags) throws RemoteException {
      // TODO Auto-generated method stub
      //将ServiceActivity中传递过来的数据取出
      String str = data.readString();
      Log.d("MyDebug", str);
      reply.writeString("reply Service reply");
      return super.onTransact(code, data, reply, flags);
    }

  FirstService类中的onBind()方法需返回MyBinder对象,定义如下:

    @Override
    public IBinder onBind(Intent intent) {
      // TODO Auto-generated method stub
      return new MyBinder();
    }

原文地址:https://www.cnblogs.com/zhanglei93/p/4676568.html