基础二:service生命周期

来源:互联网 发布:深圳软件产业基地招聘 编辑:程序博客网 时间:2024/05/21 22:29

了解顺序:

  1. service生命周期,启动,停止
  2. service与activity的连接建立:绑定,解绑
  3. service耗时操作
  4. service后台与前台

首先,service生命周期:
service生命周期


如图为service启动与绑定生命周期。

public class MyService extends Service {    final static String TAG = "MyService";    @Override    public void onCreate() {       DebugLog.i( "onCreate");        super.onCreate();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        DebugLog.i("onStartCommand");        return super.onStartCommand(intent, flags, startId);    }    @Override    public IBinder onBind(Intent intent) {        Log.i(TAG, "onBind");        return new MyBinder();    }    @Override    public void onDestroy() {        Log.i(TAG, "onDestroy");        super.onDestroy();    }    @Override    public void onRebind(Intent intent) {        Log.i(TAG, "onRebind");        super.onRebind(intent);    }        @Override    public boolean onUnbind(Intent intent) {        DebugLog.i("onUnbind");        return super.onUnbind(intent);    }

例子
如上图为测试例子,点击启动,停止按钮即可运行和停止service

@OnClick(R.id.start_service)    void go2Service(){        Intent intent=new Intent(this, MyService.class);        this.startService(intent);    }    @OnClick(R.id.stop_service)    void stopService(){        Intent intent=new Intent(this, MyService.class);        this.stopService(intent);    }

2.以上生命周期activity和service没有建立连接,只是activity启动服务。那么接下来就需要activity和service进行绑定。

 - 在activity上:    private  MyServiceConnection connection;     private void initView() {         connection=new MyServiceConnection();    }  @OnClick(R.id.bind_service)    void bindService(){        Intent intent=new Intent(this, MyService.class);        this.bindService(intent,connection, Context.BIND_AUTO_CREATE);        //Bing_auto_create表示activity和service建立关联之后会创建service的实例,并运行onCreate方法    }    @OnClick(R.id.unbind_service)    void unBindService(){        unbindService(connection);    }

其中,首先点击按钮,通过bindService()方法,绑定服务有一个建立连接的类MyServiceConnection。这里需要注意:绑定和解绑方法里的ServiceConnection对象需要为同一个对象。

@Override    public IBinder onBind(Intent intent) {        Log.i(TAG, "onBind");        return new MyBinder();    }

调用onBind方法,放回Binder对象,需要注意的是,多个客户端可以绑定一个service,但是onBind方法只会被调用一次,由第一个client绑定时调用,其他客户端绑定只得到第一次绑定后得到的binder对象。

public class MyServiceConnection implements ServiceConnection {    @Override    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {        DebugLog.i("onServiceConnected");        ((MyService.MyBinder) iBinder).getService();    }    @Override    public void onServiceDisconnected(ComponentName componentName) {        DebugLog.i("onServiceDisconnected");    }}

在OnServiceConnected方法上调用IBinder类的getService()方法。

      public class MyBinder extends Binder {        public MyService getService() {            //开启子线程执行耗时操作            DebugLog.i("getService");            return MyService.this;        }    }

然后在getService方法中执行耗时操作。Binder机制是贯穿整个android系统的进程间访问机制。注意:service是运行在主线程上的,不能直接在此方法中进行耗时操作,可能会引起ORM


service日志


以上图片为启动service,执行onCreate,onStartCommand(onStart()方法从api5就已经废除了);绑定service,执行onBind,onServiceConnected,gerService;点击停止service按钮后,未执行其他方法,因为服务未解绑;点击解绑service后,调用onDestroy。


service日志2


以上图片log日志打印出来的结果是,启动service,停止service,绑定service,解绑service。


4.耗时操作

因为service是运行在主线程的,所以不能直接在service上进行耗时操作,方法为:在Binder方法中开启子线程。有两种方法:
- 开启子线程
- service集成IntentService


  • 集成IntentService
   @OnClick(R.id.start_service)    void go2Service() {        Intent intent = new Intent(this, MyIntentService.class);        this.startService(intent);    }
public class MyIntentService extends IntentService {    public  MyIntentService(){        super("myIntentService");    }    @Override    protected void onHandleIntent(Intent intent) {        DebugLog.i("downLoading"+Thread.currentThread().getId());        try {            Thread.sleep(5000);        } catch (InterruptedException e) {            e.printStackTrace();        }        DebugLog.i("downLoaded"+Thread.currentThread().getId());    }

IntentService
图上所示为启动服务是执行过程。


  • 新建线程
    public class MyBinder extends Binder {        public MyService getService() {            //开启子线程执行耗时操作            DebugLog.i("getService 文件现在所在线程:"+Thread.currentThread().getId());            new Thread(new Runnable() {                @Override                public void run() {                    try {                        DebugLog.i("getService  downloading文件下载中线程:"+Thread.currentThread().getId());                        Thread.sleep(5000);                        DebugLog.i("getService downloaded 文件下载完成线程:" + Thread.currentThread().getId());                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }).start();            DebugLog.i("service结束"+Thread.currentThread().getId());            return MyService.this;        }    }

Myservice


上图为启动,绑定服务的流程。


4.后台service与前台service。

  • 后台service:音乐播放器
  • 前台service:例如墨迹天气,通过通知栏来显示,天气情况通过后台实时刷新。通过Notification和NotificationManager来管理。
0 0