已授予Android权限但仍拒绝权限

时间:2017-01-08 15:41:47

标签: android permissions android-6.0-marshmallow

我的项目有3个不同的活动。第一个活动包含所有权限请求。应用程序已授予所有权限,但仍无法在SD卡上写入文件。如果我关闭应用程序并再次启动,那么我可以在SD卡上写文件。无论如何还有多个活动的更新权限。

public class PermissionActivity extends AppCompatActivity {
public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
private static int TIME_OUT = 2000;
private String TAG = "tag";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_permission);

    Locale locale = new Locale("en");
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    this.getResources().updateConfiguration(config, null);

    if(checkAndRequestPermissions()) {
        // carry on the normal flow, as the case of  permissions  granted.
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity

                Intent i = new Intent(PermissionActivity.this, MainActivity.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, TIME_OUT);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    Log.d(TAG, "Permission callback called-------");
    switch (requestCode) {
        case REQUEST_ID_MULTIPLE_PERMISSIONS: {

            Map<String, Integer> perms = new HashMap<>();
            // Initialize the map with both permissions
            perms.put(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
            perms.put(android.Manifest.permission.WRITE_CALENDAR, PackageManager.PERMISSION_GRANTED);
            perms.put(android.Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
            perms.put(android.Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
            // Fill with actual results from user
            if (grantResults.length > 0) {
                for (int i = 0; i < permissions.length; i++)
                    perms.put(permissions[i], grantResults[i]);
                // Check for both permissions
                if (perms.get(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                        && perms.get(android.Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED
                        && perms.get(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        && perms.get(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "all permission granted");

                    // process the normal flow
                    Intent i = new Intent(PermissionActivity.this, MainActivity.class);
                    startActivity(i);
                    finish();
                    //else any one or both the permissions are not granted
                } else {
                    Log.d(G.mLogTag, "Some permissions are not granted ask again ");
                    if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            || ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_CALENDAR)
                            || ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
                            || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
                        showDialogOK("Service Permissions are required for this app",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        switch (which) {
                                            case DialogInterface.BUTTON_POSITIVE:
                                                checkAndRequestPermissions();
                                                break;
                                            case DialogInterface.BUTTON_NEGATIVE:
                                                // proceed with logic by disabling the related features or quit the app.
                                                finish();
                                                break;
                                        }
                                    }
                                });
                    }
                    else {
                        explain("You need to give some mandatory permissions to continue. Do you want to go to app settings?");
                    }
                }
            }
        }
    }
}

private boolean checkAndRequestPermissions(){
    int writeStoragePermission = ContextCompat.checkSelfPermission(PermissionActivity.this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
    int writeCalenderPermission = ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.WRITE_CALENDAR);
    int fineLocationPermission = ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_FINE_LOCATION);
    int coarseLocationPermission = ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_COARSE_LOCATION);

    List<String> listPermissionsNeeded = new ArrayList<>();
    if (writeStoragePermission != PackageManager.PERMISSION_GRANTED)
        listPermissionsNeeded.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (writeCalenderPermission != PackageManager.PERMISSION_GRANTED)
        listPermissionsNeeded.add(android.Manifest.permission.WRITE_CALENDAR);
    if (fineLocationPermission != PackageManager.PERMISSION_GRANTED)
        listPermissionsNeeded.add(android.Manifest.permission.ACCESS_FINE_LOCATION);
    if (coarseLocationPermission != PackageManager.PERMISSION_GRANTED)
        listPermissionsNeeded.add(android.Manifest.permission.ACCESS_COARSE_LOCATION);
    if (!listPermissionsNeeded.isEmpty()){
        ActivityCompat.requestPermissions(this, listPermissionsNeeded
                        .toArray(new String[listPermissionsNeeded.size()]),
                REQUEST_ID_MULTIPLE_PERMISSIONS);
        return false;
    }
    return true;
}

private void showDialogOK(String message, DialogInterface.OnClickListener okListener) {
    new AlertDialog.Builder(this)
            .setMessage(message)
            .setPositiveButton("OK", okListener)
            .setNegativeButton("Cancel", okListener)
            .create()
            .show();
}
private void explain(String msg){
    final android.support.v7.app.AlertDialog.Builder dialog = new android.support.v7.app.AlertDialog.Builder(this);
    dialog.setMessage(msg)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    //  permissionsclass.requestPermission(type,code);
                    startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                            Uri.parse("package:fi.maxitors.mydiscgolfapp")));
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    finish();
                }
            });
    dialog.show();
}

}

以上是我的许可询问活动。对不起的英语我很抱歉。

4 个答案:

答案 0 :(得分:2)

您还需要添加READ_EXTERNAL_STORAGE权限以从设备读取数据。

我认为这与Android授予写入权限时自动提供读取权限这一事实有关,但可能因为他自己的错误在某些情况下不会这样做。实际上来自文档,

  

注意:如果您的应用使用WRITE_EXTERNAL_STORAGE权限,那么它   隐含地也有权读取外部存储器。

https://developer.android.com/training/data-storage/files.html

我遇到了在设备上安装了apk后进行测试的情况,我使用命令

授予了权限

adb shell pm grant com.blippar.ar.android.debug android.permission.WRITE_EXTERNAL_STORAGE

触发了您观察到的行为。而是同时给出前一个和

adb shell pm grant com.blippar.ar.android.debug android.permission.READ_EXTERNAL_STORAGE

解决了这个问题。

答案 1 :(得分:2)

默认情况下,面向Android 10(API级别29)及更高版本的应用将获得对外部存储设备(或范围存储)的范围访问。此类应用只能看到其特定于应用的目录

  1. 临时修复-

    <清单...> ...

  2. 永久修复- openFileDescriptor

答案 2 :(得分:0)

我遇到了同样的问题,一旦升级到Gradle版本3.1.0,我就注意到了这一点。如果升级,请尝试恢复为3.0.1。

答案 3 :(得分:0)

在选定的构建版本中可能会出现问题。 场景是:

  1. 您在RELEASE中构建应用,然后运行,批准权限,编写文件
  2. 在DEBUG中重建应用程序,然后运行(通过卸载较早的RELEASE变体,因此UID可能会更改),然后尝试访问(1)中填充的文件
  3. 更改了UID,不拥有文件(路径),拒绝访问。