Service系列:Service创建和生命周期

Posted zhangjin1120

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Service系列:Service创建和生命周期相关的知识,希望对你有一定的参考价值。

  • 新建Service,AS4.0.2上的操作:右键包名-》new ->Service->Service,填写Service类名,点击finish,就好了。在androidManifest.xml中会自动生成:
   <service
            android:name=".TestService"
            android:enabled="true"
            android:exported="true"></service>
  • Service.java是一个抽象类,必须重写public abstract IBinder onBind(Intent intent);,所以默认的Service代码如下:
public class TestService extends Service {
    public TestService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

我先用ContextWrapper.startService(Intent service)方式,来启动Servcie,看看生命周期,所以仅有onBind是不够的,还要增加两个生命周期函数onCreate(),onStartCommand(),如下:

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate: ");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: ");
        return super.onStartCommand(intent, flags, startId);
    }

启动service代码:

    public void btnStartService(View view) {
        startService(new Intent(this, TestService.class));
    }

根据官方文档的描述,多次调用startServcie,并不会产生多个Service对象,这一点和startActivity(Intent intent)不同。点击两次,log结果:
在这里插入图片描述

  • 在Activity中停止Service:stopService(new Intent(this, TestService.class));, Service中,同样有onDestroy()函数。

使用bindService启动

  • 先准备ServiceConnection.java 对象,作为参数。直接贴出完整代码,清晰明了。
public class MainActivity extends AppCompatActivity {
    private TestService.DownloadBinder binder;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = (TestService.DownloadBinder) service;
            binder.startDownload();
            binder.getProgress();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    
    public void btnBindService(View view) {
        Intent intent = new Intent(this, TestService.class);
        bindService(intent,serviceConnection, BIND_AUTO_CREATE);
    }

    public void btnUnbindService(View view) {
        unbindService(serviceConnection);
    }

}
  • TestService完整代码:
public class TestService extends Service {
    
    public TestService() {
    }

    private static final String TAG = "xxx";
    class DownloadBinder extends Binder {
        public void startDownload() {
            Log.i(TAG, "startDownload: ");
        }

        public int getProgress() {
            Log.i(TAG, "getProgress: ");
            return 0;
        }
    }

    private DownloadBinder mBinder = new DownloadBinder();
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate: ");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: ");
        return super.onStartCommand(intent, flags, startId);
    }

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


    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "onUnbind: ");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "onDestroy: ");
        super.onDestroy();
    }
}

点击按钮启动Service:

    public void btnBindService(View view) {
        Intent intent = new Intent(this, TestService.class);
        bindService(intent,serviceConnection, BIND_AUTO_CREATE);
    }

输出的log:
在这里插入图片描述

  • 解绑服务:
    public void btnUnbindService(View view) {
        unbindService(serviceConnection);
    }

log输出:
在这里插入图片描述

以上是关于Service系列:Service创建和生命周期的主要内容,如果未能解决你的问题,请参考以下文章

Service学习笔记01-启动方式与生命周期

Service学习笔记01-启动方式与生命周期

Service学习笔记01-启动方式与生命周期

Service的生命周期

Service的生命周期

什么是Service以及描述下它的生命周期。Service都有哪些启动方法,有什么区别,怎样停用Service?