关于安卓8.0对后台服务限制的介绍,详情能够查看如下文章:android
Android 8.0 + Service开启方式兼容处理git
阅读文章后,咱们知道了要如何对Android8.0 Service进行处理,主要有两点:github
1.启动服务的api,若是是安卓8.0及以上则须要调用startForegroundService(intent),8.0如下则是使用startService(intent);api
2.若是是安卓8.0及以上,在Service建立5秒内须要调用startForeground(channelId, notification) 将其切换成前台服务,此时会弹出通知栏提示用户;bash
3.若是你使用的compileSdkVersion版本是28或以上(即安卓9.0或以上),则须要在Manifest.xml中声明如下权限:app
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
复制代码
这是一个针对安卓8.0对后台服务的限制,对Service作出了兼容的框架,本篇主要也是为了介绍AndroidOServiceCompat框架的使用,使用AndroidOServiceCompat框架,可让你的项目的Service更快更方便地兼容安卓8.0。框架
1、将项目中使用startService()启动服务的方式,更改成调用ServiceCompat.startService(context,intent)maven
2、将项目中继承Service的类改为继承ServiceCompat,如demo中的VideoUploadService:ide
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()方法中作处理的:gradle
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…