繁体   English   中英

Android权限-关于权限请求冲突

[英]Android permissions - regarding permission request conflicts

我有以下代码来请求活动的onCreate()READ_EXT_STORAGE权限:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                // 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.
                AlertDialog.Builder adb = new AlertDialog.Builder(this);
                adb.setMessage("This app needs to read your External Storage/Gallery to provide the images and the music you're going to use in the video.").setPositiveButton("OK", null).create();
                adb.show();
            } else {

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

                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_READ_EXT_STORAGE);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }
        else
        {
            onCreateActual();
        }
    }

其中onCreateActual()包含要在onCreate()执行的实际代码。 它包括动态设置片段。

onStartActual()涉及读取外部存储中的相册名称,并将它们放在片段的Lis​​tView中。

这是我的onRequestPermissionsResult()

private int MY_PERMISSIONS_REQUEST_READ_EXT_STORAGE = 102;

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Log.i("perms", permissions.length+"");
        int indexOfReadStorage = 0;
        for(int i = 0 ; i < permissions.length; i++)
        {
            Log.i("perms_len", permissions[i]);
            if(permissions[i].contains("READ_EXTERNAL_STORAGE"))
            {
                indexOfReadStorage = i;
            }
        }
        if (grantResults.length > 0 && grantResults[indexOfReadStorage] == PackageManager.PERMISSION_GRANTED) {
            // Permission Granted
            Log.i("read_ext_storage_perm", "granted");
            onCreateActual();
        } else {
            // Permission Denied
            Toast.makeText(this, "READ_EXT_STORAGE Denied", Toast.LENGTH_SHORT)
                    .show();
        }
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

我的问题是我有一个广告库正在请求READ_EXTERNAL_STORAGE权限,如果我在此之前放置onCreateActual()而不是在else子句中,则该库会首先请求该权限,而上面的onRequestPermissionsResult()不会叫。

但是,如果我这样做,则getSupportFragmentManager().beginTransaction().add(R.id.folders_section, ctrlFragOne, "album_search_results").add(R.id.selection_section, ctrlFragTwo, "indicator").commit(); 网我:

java.lang.IllegalStateException:onSaveInstanceState之后无法执行此操作

因此,除非有更好的方法,否则我认为我必须暂停活动的生命周期,直到授予READ_EXTERNAL_STORAGE权限为止。

是否有可能使活动的生命周期停滞不前,例如,更改变量? 如果没有,我可以刷新片段吗?

我认为您应该尝试询问效果很好并消除了大部分麻烦

只是编译依赖

compile 'com.vistrav:ask:2.1'

这是完整的例子

public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Ask.on(this)
            .forPermissions(Manifest.permission.ACCESS_COARSE_LOCATION
                    , Manifest.permission.WRITE_EXTERNAL_STORAGE)
            .withRationales("Location permission need for map to work properly",
                    "In order to save file you will need to grant storage permission") //optional
            .go();
}

//optional
@AskGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)
public void fileAccessGranted() {
    Log.i(TAG, "FILE  GRANTED");
}

//optional
@AskDenied(Manifest.permission.WRITE_EXTERNAL_STORAGE)
public void fileAccessDenied() {
    Log.i(TAG, "FILE  DENiED");
}

//optional
@AskGranted(Manifest.permission.ACCESS_COARSE_LOCATION)
public void mapAccessGranted() {
    Log.i(TAG, "MAP GRANTED");
}

//optional
@AskDenied(Manifest.permission.ACCESS_COARSE_LOCATION)
public void mapAccessDenied() {
    Log.i(TAG, "MAP DENIED");
}
}

您可以在这里找到更多详细信息

希望能帮助到你

暂无
暂无

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

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