Service

前言

  • 本篇主要介绍 Service 的启动方式、生命周期、与 Activity 通信、前台服务和 IntentServie

正文

一、服务的启动方式和生命周期

由于启动方式不同,生命周期就会略有不同,所以就写在一块了

1.1 启动方式有两种
  • 第一种:
Intent intent = new Intent(this, MyService.class);
startService(intent);

这种开启服务方式,当服务开启之后,与开启服务的页面无关,服务会长期在后台运行

  • 第二种:
Intent intent = new Intent(this, MyService.class);
bindService(intent,null,BIND_AUTO_CREATE);

绑定服务方式,随着页面的打开而开启服务,随着页面的关闭而停止服务。

这种方式主要是为了 Activity 与 Service 之间通信使用的

  • 第一种启动模式对应的生命周期方法
第一次服务开启,系统会自动运行 onCreat() 和 onStartcommand() 方法。
    
再次服务开启的话,系统只会运行 onStartcommand() 方法。

当停止服务时,系统会自动运行 onDestroy()方法。

而停止服务,在正常情况下,是在系统内存不足的情况下自动执行。
或者是,由用户在应用管理内手动停止。或者是调用   stopService(intent);
  • 第二种启动方式对应的生命周期方法
第一次绑定服务,系统会自动运行 onCreat() 和 onBind() 方法。
不再走 onStartcommand() 方法。

在服务停止之前,多次绑定服务,会出错,因为每次绑定的都是一个新的 ServiceConnection,
而最后解绑的还是第一次的 ServiceConnection( ServiceConnection 为 bindService() 方法内的第二个参数)。

当停止服务时,系统会自动运行 onDestroy() 方法。

手动解除绑定调用 unbindService();

  • 另外,在服务中调用 stopSelf(); 方法可以停止服务

二、Activity 与 Service 通信

比如,现在有个需求,在 Service 中进行下载,在 Activity 中查看下载进度,并控制下载开始时间,怎么办

答案是依靠 Service 中的 onBind 方法,具体操作流程如下:

  • 第一步,在 Service 中创建 Binder 子类,并设置 onBind 方法的返回值为我们自己创建的 Binder
    public Mybinder mybinder = new Mybinder();

    class Mybinder extends Binder{
        
        //开始下载
        public void startDownLoad(){

        }
        //获取进度
        public int getDownProgress(){
            return 0;
        }

    }

    @Override
    public IBinder onBind(Intent intent) {
        return mybinder;
    }
  • 第二步,在 Activity 中以绑定服务的方式开启服务,在 ServiceConnection 中的 onServiceConnected 中,将 service 强转为 mybinder 对象,然后调用其中的方法来进行操作
public class MainActivity extends AppCompatActivity {

    private MyService.Mybinder mybinder ;

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


        ServiceConnection connection = new ServiceConnection() {
        
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {

                mybinder = (MyService.Mybinder) service;
                mybinder.startDownLoad();
                mybinder.getDownProgress();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };

        Intent intent = new Intent(this, MyService.class);
        bindService(intent,connection,BIND_AUTO_CREATE);
        stopService(intent);

其中,bindService 中的第二个参数为 ServiceConnection ,第三个参数 BIND_AUTO_CREATE 表示在 Activity 和 Service 进行绑定之后自动创建服务

三、前台服务

3.1 为什么要使用前台服务?

因为服务的优先级比较低,当系统出现内存不足的情况时,就有可能回收掉正在后台运行的服务,而使用前台服务,可以使服务一直保持运行状态,而不会由于系统内存不足导致被回收。

3.2 与后台服务的区别

前台服务会一直有一个正在运行的图标显示在状态栏上,非常类似于通知的效果,比如说,我们在下载东西的时候,有的应用就会在状态栏上显示下载进度。

3.3 使用
public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return mybinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        
        Intent intent = new Intent(this, TextActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("通知栏标题")
                .setContentText("通知栏内容")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .build();
        
        startForeground(1,notification);
    }

在 onCreat 方法中设置一个通知,并显示出来,这样就会让 Service 变成前台进程

四、IntentService

  • IntentService 是一种扩展的 Service ,是一个内部提供了异步 、自动停止功能的 Service,如下所示:
public class MyIntentService extends IntentService {


    public MyIntentService() {
        //调用父类的有参构造
        super("MyIntentService");
    }
    //此方法在子线程中执行,可以做耗时操作
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
  • 它的启动方式与 Service 一样

你可能感兴趣的:(Service)