Android Service的两种启动方式以及生命周期

Service的两种启动方式:

1.startService 
2.bindService

注意: .在Android 5.0之后google出于安全的角度禁止了隐式声明Intent来启动Service.也禁止使用Intent filter.否则就会抛个异常出来.

1.startService的启动代码和销毁代码:

public class ListViewActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View.inflate(ListViewActivity.this, R.layout.activity_list_view, contentView);
        ButterKnife.bind(this);
        Intent intent=new Intent(ListViewActivity.this, ExampleService.class);
        startService(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent serviceIntent = new Intent(this,ExampleService.class);
        stopService(serviceIntent);
    }
}


2. bindService启动代码


public class RxJavaActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rx_java);
      /*  Observable<String> myObservable=Observable.create{

        }*/
        Intent gattServiceIntent = new Intent(this, ExampleService.class);
        bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
    }

    private final ServiceConnection mServiceConnection = new ServiceConnection(){

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mServiceConnection);
    }
}

注意:OnDestroy方法中需要写   unbindService( mServiceConnection )负责,log层会报错,但是对程序无影响,不写退出Activity也会销毁这个Service。

Service:
public class ExampleService extends Service {

    int mStartMode;
    IBinder mBinder;
    boolean mAllowRebind;

    public ExampleService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("ExampleService","onCreate()");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.d("ExampleService", "onStart()");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("ExampleService", "onStartCommand()");
        return mStartMode;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        Log.d("ExampleService", "onBind()");
        return mBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d("ExampleService", "onUnbind()");
        return mAllowRebind;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("ExampleService", "onDestroy()");
    }

}

startService启动销毁log

01-28 20:44:56.901 2047-2047/com.langtian.matchlayout D/ExampleService: onCreate()
01-28 20:44:57.025 2047-2047/com.langtian.matchlayout W/EGL_genymotion: eglSurfaceAttrib not implemented
01-28 20:44:57.029 2047-2047/com.langtian.matchlayout D/ExampleService: onStartCommand()
01-28 20:45:13.881 2047-2047/com.langtian.matchlayout W/EGL_genymotion: eglSurfaceAttrib not implemented
01-28 20:45:14.781 2047-2047/com.langtian.matchlayout D/ExampleService: onDestroy()


bindService启动销毁的log

01-28 20:46:03.373 2047-2047/com.langtian.matchlayout W/EGL_genymotion: eglSurfaceAttrib not implemented
01-28 20:46:03.897 2047-2047/com.langtian.matchlayout I/Choreographer: Skipped 31 frames!  The application may be doing too much work on its main thread.
01-28 20:46:13.461 2047-2047/com.langtian.matchlayout W/EGL_genymotion: eglSurfaceAttrib not implemented
01-28 20:46:14.161 2047-2047/com.langtian.matchlayout I/Choreographer: Skipped 44 frames!  The application may be doing too much work on its main thread.
01-28 20:46:15.205 2047-2047/com.langtian.matchlayout D/ExampleService: onUnbind()
01-28 20:46:15.205 2047-2047/com.langtian.matchlayout D/ExampleService: onDestroy()

startService启动退出时不销毁,再bindService启动的log

01-28 20:47:46.157 3905-3905/com.langtian.matchlayout D/ExampleService: onCreate()
01-28 20:47:46.157 3905-3905/com.langtian.matchlayout D/ExampleService: onStartCommand()
01-28 20:47:48.621 3905-3905/com.langtian.matchlayout W/EGL_genymotion: eglSurfaceAttrib not implemented
01-28 20:47:50.229 3905-3905/com.langtian.matchlayout W/EGL_genymotion: eglSurfaceAttrib not implemented
01-28 20:47:50.229 3905-3905/com.langtian.matchlayout D/ExampleService: onBind()
01-28 20:47:50.813 3905-3905/com.langtian.matchlayout I/Choreographer: Skipped 35 frames!  The application may be doing too much work on its main thread.
01-28 20:47:53.441 3905-3905/com.langtian.matchlayout W/EGL_genymotion: eglSurfaceAttrib not implemented
01-28 20:47:54.145 3905-3905/com.langtian.matchlayout I/Choreographer: Skipped 44 frames!  The application may be doing too much work on its main thread.
01-28 20:47:55.345 3905-3905/com.langtian.matchlayout D/ExampleService: onUnbind()

注意:启动bindService没有再执行OnCreate方法,所以onCreate方法只执行一次。

服务运行中,只有onStartCommand()可多次调用,其他在一个生命周期只调用一次。

startService的生命周期
onCreate()------>onStartCommand------->running----->stopService()/stopSelf()----->onDestroy()

bindService的生命周期

onCreate()------->onBind()------->running------>onUnbind()------->onDestroy()

先启动startService后启动BindService的生命周期

onCreate()------>onStartCommand------->running------->onBind()------->running------>onUnbind()------->onDestroy()

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值