Android 学习笔记之——service进一步探索

之前博客《Android学习笔记之——Service 》已经初步的学习了service了。本博文继续深入的学习一下。

目录

service工作周期

使用前台服务

使用IntentService


service工作周期

之前博文《Android学习笔记之——Service 》用到的onCreate() 、onStartCommand() 、onBind() 和onDestroy() 等方法都是service工作周期中可能回调的方法。一旦在项目的任何位置调用了Context的startService() 方法,相应的服务就会启动起来,并回调onStartCommand() 方法。如果这个服务之前还没有创建过,onCreate() 方法会先于onStartCommand() 方法执行。服务启动了之后会一直保持运行状态,直
到stopService() 或stopSelf() 方法被调用。

注意,虽然每调用一次startService()方法,onStartCommand() 就会执行一次,但实际上每个服务都只会存在一个实例。所以不
管你调用了多少次startService() 方法,只需调用一次stopService() 或stopSelf()方法,服务就会停止下来了。

另外,还可以调用Context的bindService() 来获取一个服务的持久连接,这时就会回调服务中的onBind() 方法。类似地,如果这个服务之前还没有创建过,onCreate() 方法会先于onBind() 方法执行。之后,调用方可以获取到onBind() 方法里返回的IBinder 对象的实例,这样就能自由地和服务进行通信了。只要调用方和服务之间的连接没有断开,服务就会一直保持运行状态。

当调用了startService() 方法后,又去调用stopService() 方法,这时服务中的onDestroy() 方法就会执行,表示服务已经销毁了。类似地,当调用了bindService() 方法后,又去调用unbindService() 方法,onDestroy() 方法也会执行,这两种情况都很好理解。

但是需要注意,我们是完全有可能对一个服务既调用了startService() 方法,又调用了bindService() 方法的,这种情况下该如何才能让服务销毁掉呢?根据Android系统的机制,一个服务只要被启动或者被绑定了之后,就会一直处于运行状态,必须要让以上两种条件同时不满足,服务才能被销毁。所以,这种情况下要同时调用stopService() 和unbindService() 方法,onDestroy() 方法才会执行。

使用前台服务

服务几乎都是在后台运行的,一直以来它都是默默地做着辛苦的工作。但是服务的系统优先级还是比较低的,当系统出现内存不足的情况时,就有可能会回收掉正在后台运行的服务。如果你希望服务可以一直保持运行状态,而不会由于系统内存不足的原因导致被回收,就可以考虑使用前台服务。前台服务和普通服务最大的区别就在于,它会一直有一个正在运行的图标在系统的状态栏显示,下拉状态栏后可以看到更加详细的信息,非常类似于通知的效果。当然有时候你也可能不仅仅是为了防止服务被回收掉才使用前台服务的,有些项目由于特殊的需求会要求必须使用前台服务,比如说彩云天气这款天气预报应用,它的服务在后台更新天气数据的同时,还会在系统状态栏一直显示当前的天气信息。

接下来看看如何创建一个前台服务。修改myservice代码。如下所示:

package com.example.servicetest;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.ProgressBar;

import androidx.core.app.NotificationCompat;

public class MyService extends Service { //继承自service类,说明这事一个服务

    //定义一个内部类,下载,继承自Binder
    class DownloadBinder extends Binder{

        //提供了开始下载以及查看下载进度的方法

        public void startDownload(){
            Log.d("MyService", "startDownload executed");
        }

        public int getProgress() {
            Log.d("MyService", "getProgress executed");
            return 0;
        }
    }

    //创建了DownloadBinder的实例
    private DownloadBinder mBinder= new DownloadBinder();


    public MyService() {
        
    }

    @Override
    public IBinder onBind(Intent intent) {//这是service中唯一一个抽象方法,所以必须在子类中实现
        // TODO: Return the communication channel to the service.
//        throw new UnsupportedOperationException("Not yet implemented");
        return mBinder;//在onBind() 方法里返回前面创建的DownloadBinder实例
    }

    
    
    //onCreate() 方法会在服务创建的时候调用
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("MyService", "onCreate executed");//加入打印日志

        Intent intent=new Intent(this, MainActivity.class);
        PendingIntent pi=PendingIntent.getActivity(this, 0,intent,0);
        
        //创建通知。
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("This is content title")
                .setContentText("This is content text")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                        R.mipmap.ic_launcher))
                .setContentIntent(pi)
                .build();
        //调用调用了startForeground() 方法。将通知显示出来。
        startForeground(1, notification);//让MyService变成一个前台服务,并在系统状态栏显示出来。
        //第一个参数是通知的id,类似于notify() 方法的第一个参数,
        //第二个参数则是构建出的Notification 对象。
    }



    //onStartCommand() 方法会在每次服务启动的时候调用
    //如果希望服务一旦启动就立刻去执行某个动作,就可以将逻辑写在这里
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("MyService", "onStartCommand executed");//加入打印日志
        return super.onStartCommand(intent, flags, startId);
    }
    
    
    
    //onDestroy() 方法会在服务销毁的时候调用。
    //在此处回收那些不再使用的资源
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("MyService", "onDestroy executed");//加入打印日志
    }
    
}

运行程序。并点击Start Service或Bind Service按钮,MyService就会以前台服务的模式启动了,并且在系统状态栏会显示一个通知图标,下拉状态栏后可以看到该通知的详细内容,如下图所示。

这里不知道为啥根本没法像《第一行代码》那样用。一点击按钮,程序就奔溃了。。。。应该是这个Notification notification = new NotificationCompat.Builder(this)谷歌已经不支持了。加上前台服务好像也用得比较少。有知道怎么解决这个bug的伙伴,欢迎评论赐教

使用IntentService

服务中的代码都是默认运行在主线程当中的,如果直接在服务里去处理一些耗时的逻辑,就很容易出现ANR(Application Not Responding)的情况。所以这个时候就需要用到Android多线程编程的技术了,我们应该在服务的每个具体的方法里开启一个子线程,然后在这里去处理那些耗时的逻辑。因此,一个比较标准的服务就可以写成如下形式:

public class MyService extends Service {
    ...
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
            // 处理具体的逻辑
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }
}

但是,这种服务一旦启动之后,就会一直处于运行状态,必须调用stopService() 或者stopSelf() 方法才能让服务停止下来。所以,如果想要实现让一个服务在执行完毕后自动停止的功能,就可以这样写:

public class MyService extends Service {
...
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 处理具体的逻辑
                stopSelf();
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }
}

虽说这种写法并不复杂,但是总会有一些程序员忘记开启线程,或者忘记调用stopSelf() 方法。为了可以简单地创建一个异步的、会自动停止的服务,Android专门提供了一个IntentService 类,这个类就很好地解决了前面所提到的两种尴尬,下面我们就来看一下
它的用法。

新建一个MyIntentService 类继承自IntentService ,代码如下所示:

public class MyIntentService extends IntentService {
    public MyIntentService() {
        super("MyIntentService"); // 调用父类的有参构造函数
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // 打印当前线程的id
        Log.d("MyIntentService", "Thread id is " + Thread.currentThread(). getId());
    }

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

这里首先要提供一个无参的构造函数,并且必须在其内部调用父类的有参构造函数。

然后要在子类中去实现onHandleIntent() 这个抽象方法,在这个方法中可以去处理一些具体的逻辑,而且不用担心ANR的问题,因为这个方法已经是在子线程中运行的了

根据IntentService 的特性,这个服务在运行结束后应该是会自动停止的,所以我们又重写了onDestroy() 方法,在这里也打印了一行日志,以证实服务是不是停止掉了

接下来修改activity_main.xml中的代码,加入一个用于启动MyIntentService这个服务的按钮,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/start_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start Service" />
    <Button
        android:id="@+id/stop_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop Service" />

    <Button
        android:id="@+id/bind_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Bind Service" />
    <Button
        android:id="@+id/unbind_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Unbind Service" />

    <Button
        android:id="@+id/start_intent_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start IntentService" />

</LinearLayout>

然后修改MainActivity中的代码,如下所示:

package com.example.servicetest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private MyService.DownloadBinder downloadBinder;

    //创建一个匿名类
    private ServiceConnection connection=new ServiceConnection() {
        //这两个方法会在活动与服务成功绑定以及活动与服务的连接断开的时候调用

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //通过向下转型得到了DownloadBinder 的实例
            downloadBinder=(MyService.DownloadBinder) service;

            //可以在活动中根据具体的场景来调用DownloadBinder 中的任何public 方法
            // 即实现了指挥服务干什么服务就去干什么的功能
            downloadBinder.startDownload();
            downloadBinder.getProgress();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };



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

        //分别获取到了Start Service按钮和Stop Service按钮的实例
        Button startService =(Button) findViewById(R.id.start_service);
        Button stopService =(Button) findViewById(R.id.stop_service);
        Button bindService = (Button) findViewById(R.id.bind_service);
        Button unbindService = (Button) findViewById(R.id.unbind_service);
        Button startIntentService = (Button) findViewById(R.id.start_intent_service);


        //注册点击事件
        startService.setOnClickListener(this);
        stopService.setOnClickListener(this);
        bindService.setOnClickListener(this);
        unbindService.setOnClickListener(this);
        startIntentService.setOnClickListener(this);
    }

    @Override
    public void onClick (View v){
        switch (v.getId()){

            case R.id.start_intent_service:
                //打印主线程的id
                Log.d("MainActivity","Thread is is"+Thread.currentThread().getId());
                Intent intentService=new Intent(this, MyIntentService.class);
                startService(intentService);
                break;

            case R.id.start_service://在按钮点击事件中,构建一个Intent对象
                Intent startIntent = new Intent (this, MyService.class);
                startService(startIntent);//启动服务
                break;

            case R.id.stop_service:
                Intent stopIntent=new Intent(this, MyService.class);
                stopService(stopIntent);//停止服务
                break;

            case R.id.bind_service:
                Intent bindIntent = new Intent(this, MyService.class);
                //调用bindService() 方法,将MainActivity和MyService进行绑定
                bindService(bindIntent, connection, BIND_AUTO_CREATE); // 绑定服务
                //第一个参数就是刚刚构建出的Intent 对象
                // 第二个参数是前面创建出的ServiceConnection的实例,
                // 第三个参数则是一个标志位,这里传入BIND_AUTO_CREATE 表示在活动和服务进行绑定后自动创建服务。
                break;

            case R.id.unbind_service:
                unbindService(connection); // 解绑服务
                break;

            default:
                break;
        }
    }

}

对于MyIntentService

package com.example.servicetest;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class MyIntentService extends IntentService {
    public MyIntentService() {
        super("MyIntentService"); // 调用父类的有参构造函数
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // 打印当前线程的id
        Log.d("MyIntentService", "Thread id is " + Thread.currentThread(). getId());
    }

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

最后不要忘记,服务都是需要在AndroidManifest.xml里注册的,如下所示:

运行结果如下图所示

可以看到,不仅MyIntentService和MainActivity所在的线程id不一样,而且onDestroy() 方法也得到了执行,说明MyIntentService在运行完毕后确实自动停止了。集开启线程和自动停止于一身,IntentService还是博得了不少程序员的喜爱。

猜你喜欢

转载自blog.csdn.net/gwplovekimi/article/details/105763413
今日推荐