Android android.app.backgroundservicestartnotallowedexception: not allowed to start

andorid启动服务报错

android.app.backgroundservicestartnotallowedexception: not allowed to start

解决:
加入权限

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

启动方式

// 在Activity或Service中启动服务
Intent serviceIntent = new Intent(this, MyForegroundService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    
    
    startForegroundService(serviceIntent);
} else {
    
    
    startService(serviceIntent);
}