Android8.0 Service的適配框架

安卓8.0對後臺服務的限制

關於安卓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"/>
複製代碼

AndroidOServiceCompat框架

這是一個針對安卓8.0對後臺服務的限制,對Service作出了兼容的框架,本篇主要也是爲了介紹AndroidOServiceCompat框架的使用,使用AndroidOServiceCompat框架,可讓你的項目的Service更快更方便地兼容安卓8.0。框架

效果

image

如何使用

  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…

相關文章
相關標籤/搜索