錯誤信息如下:app is in background uid null,如圖:
查了壹些帖子發現提供的2018年左右的解決方案為:
Settings->Build,Execution,Deployment->Instant Run
關掉 Enable Instant Run to hot swap code/?
嘗試失效。
後續找到解決方案:
adb shell am start-foreground-service -n com.demo.screenrecorder/com.demo.screenrecorder.RecordService
使用這個start-foreground-service來替換startservice可以解決這個問題。特此記錄。
PS:Android O 推出出了Background Execution Limits,減少後臺應用內存使用及耗電,壹個很明顯的應用就是不準後臺應用通過startService啟動服務。
解決方案就是及時調用startForeground,對於O以後的還要註意Notification需要壹個ChannelID
@Override
public void onCreate() {
super.onCreate();
try {
String CHANNEL_ONE_ID ="com.demo.screenrecorder";
String CHANNEL_ONE_NAME ="Channel One";
NotificationChannel notificationChannel =null;
notificationChannel =new NotificationChannel(CHANNEL_ONE_ID,
CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
assert manager !=null;
manager.createNotificationChannel(notificationChannel);
startForeground(1, new NotificationCompat.Builder(this, CHANNEL_ONE_ID).build());
}catch (Exception e) {
Log.e(TAG, e.getMessage());
}
initView();
}
推薦閱讀: Android O 後臺startService限制簡析