安卓8.0對后臺服務的限制
關于安卓8.0對后臺服務限制的介紹,詳情可以查看以下文章:
閱讀文章后,我們知道了要如何對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。
效果
如何使用
??一、將項目中使用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地址:https://github.com/chaychan/AndroidOServiceCompat.git