美文网首页
Service生命周期的说明

Service生命周期的说明

作者: 取了个很好听的名字 | 来源:发表于2019-08-16 16:36 被阅读0次

    前言

    本文主要说明一下service的生命周期

    生命周期方法

    在service中可以手动调用的方法为:
    startService:开启服务
    stopService: 关闭服务
    bindService:绑定服务
    unbindService:解绑服务

    自动调用的方法
    onCreate:服务创建时调用
    onStartCommand:开启服务时调用
    onBind:绑定服务时调用
    onUnbind:解绑服务时调用
    onDestory:销毁服务时调用

    官方给出的声明周期图如下:


    service生命周期.jpg

    由图上可以看出调用startService会执行的声明周期:

    onCreate->onStartCommand->服务运行->调用停止服务方法(系统杀死服务)->onDestory。

    绑定服务会执行的声明周期:

    onCreate->onBind->绑定服务->调用解绑服务方法(系统杀死服务)->onUnbind->onDestory

    常见的声明周期调用

    想要使用服务主要有两种方式:开启服务和绑定服务,两者也可以一块使用,但需要注意方法的调用问题。
    MainActivity.java:

    package com.zhqy.servicelifecycle;
    
    import android.app.Service;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.IBinder;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
        Button btn_service_start;
        Button btn_service_bind;
        Button btn_service_unbind;
        Button btn_service_stop;
        private MyService.MyBinder binder;
        private ServiceConnection serviceConnection=new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                  binder= (MyService.MyBinder) service;
            }
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
    
            }
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn_service_start=findViewById(R.id.btn_service_start);
            btn_service_bind=findViewById(R.id.btn_service_bind);
            btn_service_unbind=findViewById(R.id.btn_service_unbind);
            btn_service_stop=findViewById(R.id.btn_service_stop);
    
            btn_service_start.setOnClickListener(this);
            btn_service_bind.setOnClickListener(this);
            btn_service_unbind.setOnClickListener(this);
            btn_service_stop.setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View v) {
            Intent intent;
            switch (v.getId()){
                case R.id.btn_service_start:
                    intent=new Intent(this,MyService.class);
                    startService(intent);
                    break;
                case R.id.btn_service_bind:
                    intent=new Intent(this,MyService.class);
                    bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
                    break;
                case R.id.btn_service_unbind:
                    unbindService(serviceConnection);
                    break;
                case R.id.btn_service_stop:
                    intent=new Intent(this,MyService.class);
                    stopService(intent);
                    break;
            }
        }
    }
    

    activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.zhqy.servicelifecycle.MainActivity">
    
        <Button
            android:id="@+id/btn_service_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开启服务"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            />
        <Button
            android:id="@+id/btn_service_bind"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绑定服务"
            app:layout_constraintTop_toBottomOf="@+id/btn_service_start"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginTop="10dp"
            />
        <Button
            android:id="@+id/btn_service_unbind"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@+id/btn_service_bind"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginTop="10dp"
            android:text="解绑服务"
            />
        <Button
            android:id="@+id/btn_service_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@+id/btn_service_unbind"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:text="停止服务"
            android:layout_marginTop="10dp"
            />
    
    
    
    </android.support.constraint.ConstraintLayout>
    
    

    MyService:

    package com.zhqy.servicelifecycle;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    import android.support.annotation.Nullable;
    import android.util.Log;
    
    /**
     * Created by jackal on 2019/8/16.
     */
    
    public class MyService extends Service {
    
        @Override
        public void onCreate() {
            Log.e("MyService","onCreate");
            super.onCreate();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.e("MyService","onStartCommand");
            return super.onStartCommand(intent, flags, startId);
        }
    
    
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            Log.e("MyService","onBind");
            return new MyBinder();
        }
    
        @Override
        public boolean onUnbind(Intent intent) {
            Log.e("MyService","onUnbind");
            return super.onUnbind(intent);
        }
    
        @Override
        public void onDestroy() {
            Log.e("MyService","onDestroy");
            super.onDestroy();
        }
    
        class  MyBinder extends Binder{
    
        }
    }
    
    

    manifest.xml

       <service android:name=".MyService" />
    

    (1)开启服务->绑定服务->解绑服务->停止服务

    测试结果

    08-16 16:20:34.144 3773-3773/com.zhqy.servicelifecycle E/MyService: onCreate
    08-16 16:20:34.145 3773-3773/com.zhqy.servicelifecycle E/MyService: onStartCommand
    08-16 16:20:35.522 3773-3773/com.zhqy.servicelifecycle E/MyService: onBind
    08-16 16:20:37.115 3773-3773/com.zhqy.servicelifecycle E/MyService: onUnbind
    08-16 16:20:37.924 3773-3773/com.zhqy.servicelifecycle E/MyService: onDestroy
    
    

    (2)开启服务->停止服务

    测试结果

    08-16 16:24:12.355 4632-4632/com.zhqy.servicelifecycle E/MyService: onCreate
    08-16 16:24:12.356 4632-4632/com.zhqy.servicelifecycle E/MyService: onStartCommand
    08-16 16:24:14.084 4632-4632/com.zhqy.servicelifecycle E/MyService: onDestroy
    

    (3)开启服务->开启服务->停止服务

    测试结果

    08-16 16:25:25.163 4921-4921/com.zhqy.servicelifecycle E/MyService: onCreate
    08-16 16:25:25.172 4921-4921/com.zhqy.servicelifecycle E/MyService: onStartCommand
    08-16 16:25:25.836 4921-4921/com.zhqy.servicelifecycle E/MyService: onStartCommand
    

    从测试结果来看onCreate只会调用一次,而每当调用startService方法就会执行onStartCommand方法

    (4)绑定服务->解绑服务

    08-16 16:27:29.658 5043-5043/com.zhqy.servicelifecycle E/MyService: onCreate
    08-16 16:27:29.662 5043-5043/com.zhqy.servicelifecycle E/MyService: onBind
    08-16 16:27:31.800 5043-5043/com.zhqy.servicelifecycle E/MyService: onUnbind
    08-16 16:27:31.800 5043-5043/com.zhqy.servicelifecycle E/MyService: onDestroy
    

    从测试结果来看不论是开启服务还是绑定服务,首次都会创建service并执行onCreate方法。

    (5)绑定服务->开启服务->解绑服务->停止服务

    08-16 16:31:02.905 5944-5944/com.zhqy.servicelifecycle E/MyService: onCreate
    08-16 16:31:02.913 5944-5944/com.zhqy.servicelifecycle E/MyService: onBind
    08-16 16:31:04.720 5944-5944/com.zhqy.servicelifecycle E/MyService: onStartCommand
    08-16 16:31:07.967 5944-5944/com.zhqy.servicelifecycle E/MyService: onUnbind
    08-16 16:31:10.533 5944-5944/com.zhqy.servicelifecycle E/MyService: onDestroy
    

    绑定服务->开启服务->停止服务

    08-16 16:33:17.622 6238-6238/com.zhqy.servicelifecycle E/MyService: onCreate
    08-16 16:33:17.623 6238-6238/com.zhqy.servicelifecycle E/MyService: onStartCommand
    08-16 16:33:19.554 6238-6238/com.zhqy.servicelifecycle E/MyService: onBind
    

    从测试结果可以看出当绑定服务后必须先解绑服务才能停止服务

    注意

    当调用startService后即使调用者销毁了,服务仍然可以正常运行。
    当调用bindService后,如果调用者被销毁了,服务也会随着销毁

    相关文章

      网友评论

          本文标题:Service生命周期的说明

          本文链接:https://www.haomeiwen.com/subject/tdkqsctx.html