Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何向服务请求权限_Java_Android_Android Service - Fatal编程技术网

Java 如何向服务请求权限

Java 如何向服务请求权限,java,android,android-service,Java,Android,Android Service,我正在尝试请求对我的服务的许可。通常,为了申请许可,我必须通过一项活动。我的应用程序没有活动 // Here, thisActivity is the current activity if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { /

我正在尝试请求对我的服务的许可。通常,为了申请许可,我必须通过一项活动。我的应用程序没有
活动

// 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);
    }
}

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

向服务请求权限毫无意义。请注意,当应用程序需要访问某些系统功能时,您需要向用户请求权限,因此您必须在活动中向用户请求权限,然后启动服务。

如果您发现您的服务在启动后某个时间缺乏权限,例如,该服务由
作业调度器启动
——您可以发出
通知
提示用户授予您权限,然后安静地关闭您的服务。我的应用程序没有
活动
“从服务请求权限没有意义。”既然我的应用程序没有
活动
,那么我在
服务
@BlueNite中请求:“既然我的应用程序没有活动”——那么你就有更大的问题了。你的应用程序永远不会运行,除非它预装在某些设备上或用作其他应用程序的插件。需要使用明确的
意图
启动一个组件;在那之前,你的清单注册接收者都不能工作,其他任何东西都不能启动你的服务。为什么我被否决了?这是一个正确的问题。请解释,如果我错了,我会很高兴地删除我的问题。