简体   繁体   English

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

[英]Android permission granted but still permission denied

I have 3 different activitys on my project.我的项目有 3 个不同的活动。 First activity contains all permissions request.第一个活动包含所有权限请求。 App has all permissions granted and still cannot write file on sdcard.应用程序已授予所有权限,但仍然无法在 sdcard 上写入文件。 If I close app and starting that again then I can write file on sdcard.如果我关闭应用程序并再次启动它,那么我可以在 sdcard 上写入文件。 Is there anyway update permissions for multiple activity.无论如何更新多个活动的权限。

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

} }

Above is my permission asking activity.以上是我的许可请求活动。 I'm sorry for the bad English.我很抱歉英语不好。

Apps targeting Android 10 (API level 29) and higher are given scoped access into an external storage device, or scoped storage, by default.默认情况下,面向 Android 10(API 级别 29)及更高版本的应用会获得对外部存储设备或范围存储的范围访问。 Such apps can see only their app-specific directory此类应用程序只能看到其特定于应用程序的目录

  1. Temp Fix-临时修复-

    <manifest ... > <application android:requestLegacyExternalStorage="true" ... > ... <manifest ... > <application android:requestLegacyExternalStorage="true" ... > ...

  2. permanent Fix- openFileDescriptor永久修复-openFileDescriptor

You need to also add READ_EXTERNAL_STORAGE permission to read data from the device.您还需要添加 READ_EXTERNAL_STORAGE 权限以从设备读取数据。

I think this has to do with the fact that Android gives automatically read permission when write ones are granted, but probably for his own bug doesn't do it in certain circumstances.我认为这与Android在授予写入权限时自动授予读取权限有关,但可能因为他自己的错误在某些情况下不会这样做。 In fact from documentation,事实上,从文档来看,

Note: If your app uses the WRITE_EXTERNAL_STORAGE permission, then it implicitly has permission to read the external storage as well.注意:如果您的应用程序使用 WRITE_EXTERNAL_STORAGE 权限,那么它也隐式具有读取外部存储的权限。

at page https://developer.android.com/training/data-storage/files.html在页面https://developer.android.com/training/data-storage/files.html

I had a situation where for testing after having installed the apk on the device, i granted permission with the command我有一种情况,在设备上安装了 apk 后进行测试,我授予了命令的权限

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

that triggered the behaviour you observed.这触发了你观察到的行为。 Instead giving both the previous and the取而代之的是同时给出前一个和

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

solved the issue.解决了这个问题。

I have been having the same issue and I noticed this as soon as I upgraded to Gradle version 3.1.0.我一直有同样的问题,我在升级到 Gradle 3.1.0 版后就注意到了这一点。 Try reverting to 3.0.1 if you upgraded.如果您升级了,请尝试恢复到 3.0.1。

May be issue in the selected build variant.可能是所选构建变体中的问题。 Scenario is:场景是:

  1. you build app in RELEASE, then run, approve the permission, write a file您在 RELEASE 中构建应用程序,然后运行,批准权限,写入文件
  2. rebuild app in DEBUG, then run (with deinstall earlier RELEASE variant, so UID may change), then try to access the file populated in (1)在 DEBUG 中重建应用程序,然后运行(卸载较早的 RELEASE 变体,因此 UID 可能会更改),然后尝试访问填充在(1)中的文件
  3. UID changed, file(path) not owned, denial access occurs. UID 已更改,文件(路径)未拥有,发生拒绝访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM