Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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
Android M权限:checkSelfPermission()始终返回-1_Android_Permissions - Fatal编程技术网

Android M权限:checkSelfPermission()始终返回-1

Android M权限:checkSelfPermission()始终返回-1,android,permissions,Android,Permissions,在我的应用程序中,我有一个需要多个权限的后台服务。 由于操作系统要求开发人员确保这些权限在运行时得到批准,因此我尝试使用以下代码检查权限 getApplicationContext().checkSelfPermission() 我没有因为得到允许/拒绝而出现任何小狗。相反,我在应用程序信息页面(在设置>应用程序>应用程序管理器>我的后台服务)中允许这些权限 但是尽管我在应用程序信息页面中启用了这些权限, 上述函数始终返回-1 如果我在“应用程序信息”页面上更改了权限审批,如何正确检查权限

在我的应用程序中,我有一个需要多个权限的后台服务。 由于操作系统要求开发人员确保这些权限在运行时得到批准,因此我尝试使用以下代码检查权限

getApplicationContext().checkSelfPermission()
我没有因为得到允许/拒绝而出现任何小狗。相反,我在应用程序信息页面(在设置>应用程序>应用程序管理器>我的后台服务)中允许这些权限 但是尽管我在应用程序信息页面中启用了这些权限, 上述函数始终返回-1

如果我在“应用程序信息”页面上更改了权限审批,如何正确检查权限

 public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;

public boolean checkLocationPermission() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

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

            // Show an expanation 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.

            //Prompt the user once explanation has been shown
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);


        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
        }
        return false;
    } else {
        return true;
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                buildGoogleApiClient();
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                mGoogleMap.setMyLocationEnabled(true);

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}
你应该这样做