删除Marshmallow中当前位置的运行时权限

时间:2016-06-24 05:23:40

标签: java android android-6.0-marshmallow

我正在尝试创建一个应用。应用程序询问Marsh Mallow中的每次位置访问,始终在我们打开应用程序时。请提出解决问题的最佳流程。使用以下代码 -

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
//for marshmallow
    final int LOCATION_PERMISSION_REQUEST_CODE = 100;
     ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION},
            LOCATION_PERMISSION_REQUEST_CODE);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    mMap.setMyLocationEnabled(true);
    buildGoogleApiClient();
    mGoogleApiClient.connect();

}

感谢。

3 个答案:

答案 0 :(得分:0)

请自行反映您的代码。考虑请求和检查权限的顺序。在您要求之前,您应该检查权限。这样可以防止询问是否已授予权限...

查看example provided by Google并考虑根据您的需求调整此示例:

// 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 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.

    } else {

        // No explanation needed, we can request the permission.

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

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

此外,您应该覆盖回调方法onRequestPermissionsResult以处理用户的grantResults

// Callback with the request from calling requestPermissions(...)
@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String permissions[],
                                       @NonNull int[] grantResults) {
    // Make sure it's our original READ_CONTACTS request
    if (requestCode == READ_CONTACTS_PERMISSIONS_REQUEST) {
        if (grantResults.length == 1 &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "Read Contacts permission granted", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Read Contacts permission denied", Toast.LENGTH_SHORT).show();
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

编辑:

这是您应该更改的代码

ActivityCompat.requestPermissions(this, new String[]    {android.Manifest.permission.ACCESS_FINE_LOCATION,     android.Manifest.permission.ACCESS_COARSE_LOCATION},
        LOCATION_PERMISSION_REQUEST_CODE);

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

    return;
}

如需进一步阅读,请查看文章Understanding App Permissions

答案 1 :(得分:0)

尝试写入if else,如果没有给出权限请求它,或者做你想做的事情。

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if(checkSelfPermission(Manifest.permission.Manifest.permission.ACCESS_FINE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[]{Manifest.permission.Manifest.permission.ACCESS_FINE_LOCATION},
                            LOCATION_PERMISSION_REQUEST_CODE);
                }
            } else {
               // do your stuff
            }

并覆盖onRequestPermissionsResult并在您授予权限后应用代码。

答案 2 :(得分:0)

以下代码检查应用是否具有使用位置的权限,并在必要时请求权限:

IObservable<IEnumerable<int>> query = 
    source
        .Scan(Enumerable.Empty<int>(), (a, x) =>
            a.Concat(new [] { x }).TakeLast(4).ToList());

以下是回调方法:

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

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
    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.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
        MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION_CODE);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}
else
{
    // Permission already granted ... This is where you can continue your further business logic...
}

有关详细说明,请访问here(Android官方网页)或this(Tutsplus教程)