android Service的使用方式

2021-05-18  本文已影响0人  XQSY

Service介绍

Service是Android四大组件之一,是一个后台运行的组件,执行长时间运行且不需要用户交互的任务。即使应用被销毁也依然可以工作。

生命周期

image.png

上图展示了Service的生命周期过程,Service有两种启动方式,对应了两种不同的生命周期展现。

Service的创建和使用

创建Service
public class MyService extends Service {
    @Override
    public ComponentName startService(Intent service) {
        Log.d("MyService", "startService()");
        return super.startService(service);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d("MyService", "onBind()");
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("MyService", "onCreate()");
        mBinder = new MyBinder();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("MyService", "onDestroy()");
    }

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

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d("MyService", "onUnbind()");
        return super.onUnbind(intent);
    }

    public class MyBinder extends Binder {
        public void onDone() {
            Log.d("MyService", "onDone()");
        }
    }
//MainActivity.java
 textView.setOnClickListener(v -> startServices());
Intent serviceIntent;
 private void startServices() {
     serviceIntent = new Intent(this, MyService.class);
     startService(serviceIntent);
    }

在Activity销毁时,销毁服务

  @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(serviceIntent);
    }
//MainActivity.java

 textView.setOnClickListener(v -> startServices());
  private MyService.MyBinder myBinder;
//创建服务连接
 private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder = (MyService.MyBinder) service;
            Log.d("MainActivity", "onServiceConnected");
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {}
    };

 private void startServices() {
     Intent serviceIntent = new Intent(this, MyService.class);
     bindService(serviceIntent, mConnection, BIND_AUTO_CREATE);
    }

onBind的方式,需要创建一个ServiceConnection,用来和服务进行连接。

在Activity销毁时,断开链接。

  @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mConnection);
    }

⚠️别忘了在androidManifest.xml注册Service

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

Service和Activity的交互

public class MyService extends Service {
    MyBinder mBinder;
    @Override
    public IBinder onBind(Intent intent) {
        Log.d("MyService", "onBind()");
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("MyService", "onCreate()");
        mBinder = new MyBinder();
    }
    //服务中定义的方法
    public void onServiceDone(String param){
        Log.d("MyService", "service execute param = " + param);
    }

    public class MyBinder extends Binder {
        public void onDone(String param) {
           onServiceDone(param);
        }
    }
//MainActivity.java
//通过从Service中获取的Binder来执行方法
 textView.setOnClickListener(v -> myBinder.onDone("Activity param"));

  private MyService.MyBinder myBinder;
//创建服务连接
 private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //拿到Service中的Binder对象
            myBinder = (MyService.MyBinder) service;
        }
    };

2、或者是通过Service中的binder获取到Service对象本身

    public class MyBinder extends Binder {
        public MyService getService() {
           return MyService.this;
        }
    }
textView.setOnClickListener(v -> 
    //通过binder获取到Service对象 然后直接调用方法
    myBinder.getService(). onServiceDone("Activity param")
);

  private MyService.MyBinder myBinder;
//创建服务连接
 private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //拿到Service中的Binder对象
            myBinder = (MyService.MyBinder) service;
        }
    };
上一篇下一篇

猜你喜欢

热点阅读