Android8.0 Service的适配框架

2,547 阅读3分钟

安卓8.0对后台服务的限制

关于安卓8.0对后台服务限制的介绍,详情可以查看以下文章:

Android 8.0 + Service开启方式兼容处理

阅读文章后,我们知道了要如何对Android8.0 Service进行处理,主要有两点:

1.启动服务的api,如果是安卓8.0及以上则需要调用startForegroundService(intent),8.0以下则是使用startService(intent);

2.如果是安卓8.0及以上,在Service创建5秒内需要调用startForeground(channelId, notification) 将其切换成前台服务,此时会弹出通知栏提示用户;

3.如果你使用的compileSdkVersion版本是28或以上(即安卓9.0或以上),则需要在Manifest.xml中声明以下权限:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

AndroidOServiceCompat框架

这是一个针对安卓8.0对后台服务的限制,对Service做出了兼容的框架,本篇主要也是为了介绍AndroidOServiceCompat框架的使用,使用AndroidOServiceCompat框架,可以让你的项目的Service更快更方便地兼容安卓8.0。

效果

image

如何使用

  一、将项目中使用startService()启动服务的方式,更改为调用ServiceCompat.startService(context,intent)

  二、将项目中继承Service的类改成继承ServiceCompat,如demo中的VideoUploadService:

public class VideoUploadService extends ServiceCompat {

    @Override
    protected String getChannelName() {
        return "视频上传通知";
    }

    @Override
    protected int getChannelId() {
        return Constants.CHANNEL_ID_VIDEO_UPLOAD_SERVICE;
    }

    @Override
    public String getNotificationContent() {
        return "视频上传中...";
    }

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

    @Override
    public void onCreate() {
        //must call super.onCreate()
        super.onCreate();

        //do something
    }
}

  其中需要注意的是onCreate() 方法中一定要要调用super.onCreate() 方法,因为针对安卓8.0的兼容就是在ServiceCompat类的onCreate()方法中做处理的:

public abstract class ServiceCompat extends Service {

    @Override
    public void onCreate() {
        super.onCreate();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //适配安卓8.0
            String channelId = getChannelId() + "";
            String channelName = getChannelName();
            NotificationChannel channel = new NotificationChannel(channelId, channelName,
                    NotificationManager.IMPORTANCE_MIN);
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            manager.createNotificationChannel(channel);

            startForeground(getChannelId(), getNotification());
        }
    }
    
    ...
}

  另外,还要实现getChannelName()getChannelId()getNotificationContent() 方法,这三个方法主要返回的是通知栏的一些属性,因为将服务改为前台服务展示需要传入一个Notification,这里已经在ServiceCompat封装好一个简单的Notification,只要返回channelName,channelId以及通知的显示内容即可。

  当然,如果你想自定义通知的样式,比如修改通知的大图标largeIcon,通知的小图标smallIcon,这里默认都是使用ic_launcher,如果你想自己指定,只需要重写以下的方法:

    /**
     * Large icon for notification , subclasses can be overwritten and returned
     */
    public Bitmap getLargeIcon() {
        return BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
    }

    /**
     * Small icon for notification , subclasses can be overwritten and returned
     */
    public int getSmallIcon() {
        return R.mipmap.ic_launcher;
    }

  如果你对通知栏的样式还有更多的要求,可以重写getNotification()方法,返回你自己创建的Notification对象

 /**
     * Displayed notifications, subclasses can be overwritten and returned
     */
    public Notification getNotification() {
        return createNormalNotification(getNotificationContent());
    }

框架中默认是返回基本的通知栏,通知栏的content使用的是app_name字段,大图标和小图标使用的ic_launcher

protected Notification createNormalNotification(String content) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getChannelId() + "");
        if (TextUtils.isEmpty(content)) {
            return builder.build();
        }

        builder.setContentTitle(getString(R.string.app_name))
                .setContentText(content)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(getSmallIcon())
                .setLargeIcon(getLargeIcon())
                .build();

        return builder.build();
}

导入方式

在项目根目录下的build.gradle中的allprojects{}中,添加jitpack仓库地址,如下:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }//添加jitpack仓库地址
    }
}

打开app的module中的build.gradle,在dependencies{}中,添加依赖,如下:

dependencies {
	...
    api 'com.github.chaychan:AndroidOServiceCompat:1.0.0'
}

源码github地址:github.com/chaychan/An…