Android 8.0 啓動Service適配(Not allowed to start service Intent)

問題現象

App出現異常: java.lang.IllegalStateException: Not allowed to start service Intent  xxxx    app is in background uid UidRecord

App直接崩潰。

問題原因:

App targetSdkVersion>= 26的情況下,用戶允許App開機自啓動,App被殺死或者系統重啓後,系統直接將App後臺啓動起來,App在後臺啓動的過程中有使用startService()方法。

Google在Android 8.0之後對於處於後臺的App啓動Service進行了嚴格的限制,不再允許後臺App啓動後臺Service,如果使用會直接拋出異常。

問題調研

  1. 微信:反編譯App後,沒有找到對應Service的代碼,懷疑可能是熱加載的
  2. QQ:目前targetSdkVersion = 23,不存在此問題
  3. 小天才:使用startForegroundService方法,Notification設置channel爲null,狀態欄不顯示通知
  4. 360兒童衛士:使用startForegroundService方法,Notification正常設置,狀態欄顯示通知

解決方法

使用startForegroundService,此方法會在狀態欄顯示通知

// 啓動服務的地方
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    context.startForegroundService(new Intent(context, MyService.class));
} else {
    context.startService(new Intent(context, MyService.class));
}
// 在MyService中

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

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = null;
        channel = new NotificationChannel(CHANNEL_ID_STRING, getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(channel);
        Notification notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID_STRING).build();
        startForeground(1, notification);
    }

}

 

去除狀態欄方法

stopForeground()

//在 startForeground(1, notification);後添加如下代碼,取消前臺服務

myHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        stopForeground(true);
    }
}, 1000);

channel 設置爲null,此方法只適用於targetSdkVersion = 26的情況

Notification.Builder builder = new Notification.Builder(this, null)
.setContentTitle(getString(R.string.app_name))
.setContentText("")
.setAutoCancel(true);

Notification notification = builder.build();
startForeground(1, notification);

對於去除通知的第2種方法,源碼查看:

final class ServiceRecord extends Binder implements ComponentName.WithComponentName {
public void postNotification() {
    if (foregroundId != 0 && foregroundNoti != null) {
        ...
        ams.mHandler.post(new Runnable() {
            public void run() {
                NotificationManagerInternal nm = LocalServices.getService(NotificationManagerInternal.class);
                if (nm == null) {
                    return;
                }
                Notification localForegroundNoti = _foregroundNoti;
                try {
                    if (localForegroundNoti.getSmallIcon() == null) {
                        ...//沒有smallIcon進行處理
                    }
                    if (nm.getNotificationChannel(localPackageName, appUid, localForegroundNoti.getChannelId()) == null) {
                         int targetSdkVersion = Build.VERSION_CODES.O_MR1;
                         try {
                             final ApplicationInfo applicationInfo = ams.mContext.getPackageManager().getApplicationInfoAsUser(appInfo.packageName, 0, userId);
                             targetSdkVersion = applicationInfo.targetSdkVersion;
                         } catch (PackageManager.NameNotFoundException e) {
                         }
                         if (targetSdkVersion >= Build.VERSION_CODES.O_MR1) {
                             throw new RuntimeException("invalid channel for service notification: " + foregroundNoti);
                         }
                    }
                    nm.enqueueNotification(localPackageName, localPackageName, appUid, appPid, null, localForegroundId, localForegroundNoti, userId);

                    foregroundNoti = localForegroundNoti; // save it for amending next time
                } catch (RuntimeException e) {
                    Slog.w(TAG, "Error showing notification for service", e);
                    // If it gave us a garbage notification, it doesn't
                    // get to be foreground.
                    ams.setServiceForeground(name, ServiceRecord.this, 0, null, 0);
                    ams.crashApplication(appUid, appPid, localPackageName, -1, "Bad notification for startForeground: " + e);
                }
            }
        });
     }
}
}

從源碼中可以看出,在發送通知檢查時,如果targetSdkVersion >= Build.VERSION_CODES.O_MR1即targetSdkVersion >= 27時,如果設置channel爲null纔會拋出RuntimeException,crash當前App。

對於targetSdkVersion = 26的app會捕獲異常,正常將notification加入隊列,但此notification並不會進行顯示。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章