Services

*在实际运行中同样的Service的确只能有一个。

Services有两种启动形式:

  • Started:其他组件调用startService()方法启动一个Service。一旦启动,Service将一直运行在后台(run in the background indefinitely)即便启动Service的组件已被destroy。通常,一个被start的Service会在后台执行单独的操作,也并不给启动它的组件返回结果。比如说,一个start的Service执行在后台下载或上传一个文件的操作,完成之后,Service应自己停止。

    • 开启方式 :startService() 
      SwipeActivity.this.startService(new Intent(SwipeActivity.this, FindCardService.class));
    • 结束方式1:
      SwipeActivity.this.stopService(new Intent(SwipeActivity.this, FindCardService.class));
    • 结束方式2:
      stopSelf(); // 继承Service的子类内部调用
    • 生命周期:
      onCreate() --> onStartCommand() --> onDestroy()  // 如果Service已经启动,再次startService(),只会调用onStartCommand()方法,控件销毁时,记得解绑
  • Bound:其他组件调用bindService()方法绑定一个Service。通过绑定方式启动的Service是一个client-server结构,该Service可以与绑定它的组件进行交互。一个bound service仅在有组件与其绑定时才会运行(A bound service runs only as long as another application component is bound to it),多个组件可与一个service绑定,service不再与任何组件绑定时,该service会被destroy。

    • 开启方式:bindService()
      SwipeActivity.this.bindService(new Intent(SwipeActivity.this, FindCardService.class), connection, BIND_AUTO_CREATE);
    • 结束方式:unbindService()
      SwipeActivity.this.unbindService(connection);  // 解绑只能进行一次,不然会出现崩溃: Service not registered
    • 生命周期:
      onCreate() --> onBind() --> onDestroy();  // 如果Service已经启动,再次bindService(),没效果
    • 使用如下:
    private FindCardService.FindCardBinder mFindCardBinder;
    private boolean mBound;  // 防止调用unbindService崩溃
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            CoreLog.d(TAG, "ServiceConnection -- onServiceConnected()");
            mFindCardBinder = (FindCardService.FindCardBinder) service;
            mBound = true;
        }

        // 只有在service因异常而断开连接的时候,这个方法才会用到
        @Override
        public void onServiceDisconnected(ComponentName name) {
            CoreLog.d(TAG, "ServiceConnection -- onServiceDisconnected()");
            mBound = false;
        }
    };

绑定:

findViewById(R.id.bangding).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SwipeActivity.this.bindService(new Intent(SwipeActivity.this, FindCardService.class), connection, BIND_AUTO_CREATE);
            }
        });

解绑:

 @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBound) {
            SwipeActivity.this.unbindService(connection);
        }
    }

Service:

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        CoreLog.d(TAG, "FindCardService -- onBind()");
        return mFindCardBinder;
    }

    private FindCardBinder mFindCardBinder = new FindCardBinder();

    public class FindCardBinder extends Binder {

        public void startFindCard() {
          
        }

    }

额外情况:

如果使用了bindService和startService混合使用会是什么情况?

1.stopService()会失效,即使使用了stopService(),不会调用onDestory()方法。

其他的,会按照各自的生命周期来走。

2.当控件都解绑了,才会调用onDestory()方法。

3.在Service每一次的开启关闭过程中,只有onStart可被多次调用(通过多次startService调用)其他onCreate,onBind,onUnbind,onDestory在一个生命周期中只能被调用一次。也就是说Activity A 在onCreate()方法中bindService,正常调用onBind(),但没有在onDestory()方法中unbindService。Activity B在onCreate()方法中bindService,但没有调用onBind()方法了。

posted @ 2016-12-15 15:49  H_bolin  阅读(370)  评论(0编辑  收藏  举报