如何从服务请求权限

时间:2017-04-09 20:56:22

标签: java android android-service

我正在尝试在服务上申请权限。通常要请求权限我必须传递一个Activity。我的申请没有Activity

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {    
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);
    }
}

如何从服务中进行此操作?

1 个答案:

答案 0 :(得分:2)

请求获得服务许可是没有意义的。请注意,当应用程序需要访问某些系统功能时,您会向用户请求权限,因此您必须在活动中请求用户许可,然后启动您的服务。