我想通过给我的应用程序提供许可模型来实现新的棉花糖支持。 现在我已经在我的清单文件中给出了下面的权限。棉花糖新的许可模式
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
现在,在一个活动我已加载在其中我具有recyclerview和数据使用recyclerview适配器在此recyclerview加载的片段。
在这种recyclerview适配器的列表中,我是个有一个按钮,按下哪些我将要求上述许可。所以,当用户点击这个按钮时,我已经调用了下面的函数。
public void checkForManifestPermission(){
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(mContext,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity)mContext,
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.
ActivityCompat.requestPermissions((Activity)mContext,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
ConstantVariables.READ_EXTERNAL_STORAGE);
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions((Activity)mContext,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
ConstantVariables.READ_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
现在在活动中我重写了下面的回调方法。
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case ConstantVariables.READ_EXTERNAL_STORAGE: {
// 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.
ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE = true;
} else {
ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE = false;
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
在我打电话的功能checkForManifestPermission和检查的条件 if(ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE){}
然后只重定向到其他活动适配器。
现在我的问题是,这里面的条件编写的代码调用onRequestPermissionsResult方法之前得到执行,我想,如果允许用户授予那么只有它应该重定向到其他活动。
我怎样才能做到这一点,我想我的代码,以等待用户给出回应有关批准或拒绝的权限,然后进行进一步的相应。
非常感谢您的帮助。
请更新完整的代码。 –
尝试这可能是工作http://stackoverflow.com/a/41221852/5488468 –