Android Service用法总结和生命周期详解

Service android的四大组件之一,最重要的后台运行控件。
1.使用Service的方法 bindService 和 startService

Intent intent = new Intent(this, MyService.class);
 intent.putExtra("comid",200);
// bindService(intent,connection, Context.BIND_AUTO_CREATE);
 startService(intent);

总结:
1、不管用什么方式打开Service,service只会Oncreate一次。
2、bindService方式打开的service不会调用 onStart方法,只会调用onbind和unbindService方法
3、startService打开的Service会调用onStartCommand
4、不管bindService多少次,onBind只会根据是否Ibinder对象为null来判断是否调用onbind
5、Service 不管用startService(后台) 还是 binder(前台) service中运行的线程都是Main,所以千万不能做耗时操作。

为什么会得到这个结果 需要对Android IPC通信 Ibinder有所了解就能明白其中的原因。
IBinder有点像TCP/IP 服务器 提供多进程之间通信。

我们来测试下生命周期:
先写一个Demo Service 将各个方法显示出来

public class MyService extends Service {
    public static final String TAG = "MyTestService";
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG,"onBind");
        return new Mybind();
    }
    @Override
    public void unbindService(ServiceConnection conn) {
        super.unbindService(conn);
         Log.d(TAG,"unbindService");
    }
    @Override
    public void onCreate() {
        Log.d(TAG,"onCreate :" );

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG,"onStartCommand :" + flags +" startid :" +startId);
        int comid = intent.getIntExtra("comid",-1);
        Log.d(TAG,"comid :" + comid);

        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy :" );
    }
    public void sayHello(){
        Log.d(TAG,"say hello");
    }
    class Mybind extends Binder{

        public MyService getService(){
            return MyService.this;
        }
    }

尝试 BindService 生命周期为:

11-21 15:24:04.901 17311-17311/application D/MyTestService: onCreate :
11-21 15:24:04.902 17311-17311/application D/MyTestService: onBind

先BindService然后在 startService 看生命周期: onCreate只会有一次。

11-21 15:25:46.136 19726-19726/? D/MyTestService: onCreate :
11-21 15:25:46.137 19726-19726/? D/MyTestService: onBind
11-21 15:25:46.138 19726-19726/? D/MyTestService: onStartCommand :0 startid :1
11-21 15:25:46.138 19726-19726/? D/MyTestService: onStart :

试试多个Activity Bind同一个Service的情况: 第一个Activity Bind过Service以后第二个去Bind直接就返回了Bind,不会在去调用onBind 方法, 证明了 onBind只是一个初始化bind的代理,当发现Bind对象不为null时直接返回不在触发此方法。

11-21 15:33:34.758 25373-25373/application D/MyTestService: onCreate :
11-21 15:33:34.759 25373-25373/application D/MyTestService: onBind
11-21 15:33:34.836 25373-25373/application D/MyTestService: .Main3ActivityonServiceConnected :application.service.MyService$Mybind@3384e154
11-21 15:33:39.284 25373-25373/application D/MyTestService: .Main4ActivityonServiceConnected  :application.service.MyService$Mybind@3384e154

Service运行的线程是否在主线程
用startService启动尝试 : 可以看到 currentThread :main ,表示了service其实是运行在主线程的。

11-21 15:37:36.914 28238-28238/? D/MyTestService: onCreate :
11-21 15:37:36.916 28238-28238/? D/MyTestService: onStartCommand :0 startid :1
11-21 15:37:36.916 28238-28238/? D/MyTestService: currentThread :main
11-21 15:37:36.916 28238-28238/? D/MyTestService: onStart :

用 Bind的方法调用试试: 结果和startService方式一样,这就是为什么不能再Service中干耗时操作的原因了。

  @Override
public void onServiceConnected(ComponentName name, IBinder service) {
    Log.d(MyService.TAG,getComponentName().getShortClassName() + "onServiceConnected :" +service);
    MyService.Mybind mybind = (MyService.Mybind) service;
    mybind.getService().sayHello();
}

结果

11-21 15:41:04.349 31083-31083/? D/MyTestService: onCreate :
11-21 15:41:04.349 31083-31083/? D/MyTestService: onBind
11-21 15:41:04.406 31083-31083/? D/MyTestService: .Main3ActivityonServiceConnected :application.service.MyService$Mybind@3870a8a7
11-21 15:41:04.406 31083-31083/? D/MyTestService: currentThread :main

你可能感兴趣的:(android,Service)