Skip to content Skip to sidebar Skip to footer

Widget Can't Start A Service On Device Boot On Android 11 Until Some Activity Is Launched

val startServiceIntent = TestService.getServiceIntent(context).let { PendingIntent.getService(context, 0, it, PendingIntent.FLAG_UPDATE_CURRENT) } val views = RemoteViews(conte

Solution 1:

After diving into the source code of Android OS, I found the following way to check whether the camera is restricted:

@RequiresApi(Build.VERSION_CODES.Q)internalfun AppOpsManager.isCameraAllowedForPackage(packageName: String): Boolean {
    val res = unsafeCheckOpNoThrow(
        AppOpsManager.OPSTR_CAMERA,
        android.os.Process.myUid(),
        packageName
    )
    return res == AppOpsManager.MODE_ALLOWED
}

Solution 2:

Based on @CommonsWare comment I need to use PendingIntent.getForegroundService() instead of PendingIntent.getService()

Post a Comment for "Widget Can't Start A Service On Device Boot On Android 11 Until Some Activity Is Launched"