Android Service启动的实践

startService()

04-18 22:11:23.011 15225-15225/com.github.androidfdm E/SampleService: onCreate
04-18 22:11:23.019 15225-15225/com.github.androidfdm E/SampleService: onStartCommand

startService()

04-18 22:11:23.011 15225-15225/com.github.androidfdm E/SampleService: onCreate
04-18 22:11:23.019 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:11:25.920 15225-15225/com.github.androidfdm E/SampleService: onStartCommand

bindService()

04-18 22:11:23.011 15225-15225/com.github.androidfdm E/SampleService: onCreate
04-18 22:11:23.019 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:11:25.920 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:11:29.075 15225-15225/com.github.androidfdm E/SampleService: onBind
04-18 22:11:29.083 15225-15225/com.github.androidfdm E/MainActivity: Im binder!
04-18 22:11:29.920 15225-15225/com.github.androidfdm E/SampleService: onStartCommand

bindService()

null

startService()

04-18 22:11:23.011 15225-15225/com.github.androidfdm E/SampleService: onCreate
04-18 22:11:23.019 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:11:25.920 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:11:29.075 15225-15225/com.github.androidfdm E/SampleService: onBind
04-18 22:11:29.083 15225-15225/com.github.androidfdm E/MainActivity: Im binder!

stopService()

null

unBindService()

04-18 22:11:23.011 15225-15225/com.github.androidfdm E/SampleService: onCreate
04-18 22:11:23.019 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:11:25.920 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:11:29.075 15225-15225/com.github.androidfdm E/SampleService: onBind
04-18 22:11:29.083 15225-15225/com.github.androidfdm E/MainActivity: Im binder!
04-18 22:13:43.525 15225-15225/com.github.androidfdm E/SampleService: onUnBind

stopService()

04-18 22:13:39.773 15225-15225/com.github.androidfdm E/SampleService: onCreate
04-18 22:13:39.781 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:13:39.919 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:13:41.507 15225-15225/com.github.androidfdm E/SampleService: onBind
04-18 22:13:41.516 15225-15225/com.github.androidfdm E/MainActivity: Im binder!
04-18 22:13:43.525 15225-15225/com.github.androidfdm E/SampleService: onUnBind
04-18 22:14:37.669 15225-15225/com.github.androidfdm E/SampleService: onDestroy

结论:

start启动的Service必须由stop来销毁,若在start之后bind了则必须unbind之后再stop,若多次bind不会执行任何回调,多次start回调用多次startcommand并且多次传入Intent!

onStartCommand()返回值

onStartCommand()方法有一个int的返回值,这个返回值标识服务关闭后系统的后续操作。

返回值有以下几种:

Service.START_STICKY,启动后的服务被杀死,系统会自动重建服务并调用on onStartCommand(),但是不会传入最后一个Intent(Service可能多次执行onStartCommand),会传入一个空的Intent,使用这个标记要注意对Intent的判空处理。这个标记适用于太依靠外界数据Intent,在特定的时间,有明确的启动和关闭的服务,例如后台运行的音乐播放。
Service.START_NOT_STICKY,启动后的服务被杀死,系统不会自动重新创建服务。这个标记是最安全的,适用于依赖外界数据Intent的服务,需要完全执行的服务。
Service.START_REDELIVER_INTENT,启动后的服务被杀死,系统会重新创建服务并调用onStartCommand(),同时会传入最后一个Intent。这个标记适用于可恢复继续执行的任务,比如说下载文件。
Service.START_STICKY_COMPATIBILITY,启动后的服务被杀死,不能保证系统一定会重新创建Service。

你可能感兴趣的:(Android Service启动的实践)