[英]Marshmallow new permission model
I want to implement new marshmallow support in application by giving the permission model to my app. 我想通过给我的应用程序权限模型来在应用程序中实现新的棉花糖支持。 Now I have given the below permission in my manifest file. 现在,我在清单文件中授予了以下权限。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Now in an activity I have loaded a fragment in which I am having an recyclerview and data is loaded in this recyclerview using recyclerview adapter. 现在在一个活动中,我加载了一个片段,其中有一个recyclerview,并且使用recyclerview适配器将数据加载到了这个recyclerview中。
In this recyclerview adapter's list, I ma having a button clicking on which I would require the above permission. 在这个recyclerview适配器的列表中,我需要单击一个按钮,我需要上面的许可。 So, I have called the below function when the user will click on this button. 因此,当用户单击此按钮时,我已调用以下函数。
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.
}
}
}
Now in the activity I have overridden the below callback method. 现在,在活动中,我重写了以下回调方法。
@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
}
}
In the adapter where I am calling function checkForManifestPermission and checked the condition if(ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE){}
Then only redirect to other activity. 在我调用函数checkForManifestPermission并检查条件if(ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE){}
的适配器中,然后仅重定向到其他活动。
Now my problem is that the code written inside this condition get executed before calling the onRequestPermissionsResult method, I want that if permission granted by user then only it should redirect to other activity. 现在,我的问题是在调用onRequestPermissionsResult方法之前执行此条件下编写的代码,我希望如果用户授予的许可只能将其重定向到其他活动。
How can I achieve this, I want my code to wait for the user to give response about approve or deny the permission and then proceed further accordingly. 我该如何实现,我希望我的代码等待用户给出有关批准或拒绝权限的响应,然后进行相应的处理。
Thanks a lot for the help. 非常感谢您的帮助。
Try This: 尝试这个:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSIONS_CODE) {
for (int i = 0; i < permissions.length; i++) {
String permission = permissions[i];
int grantResult = grantResults[i];
if (permission.equals(Manifest.permission.READ_EXTERNAL_STORAGE)) {
if (grantResult == PackageManager.PERMISSION_GRANTED) {
onPPSButtonPress();
} else {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_CODE);
}
}
}
}
}
Note: Add your Read permission code as like above. 注意:如上所述,添加您的“读取”权限代码。
You can set below code for check permission in marshmallow : 您可以在棉花糖中设置以下代码进行检查权限:
if ((int) Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider WRITE_EXTERNAL_STORAGE
if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(mActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
return;
}
}
I had similar problems in two different projects I worked on. 我在两个不同的项目中遇到过类似的问题。
In one I simply performed the check of the permission on entry to the Activity. 在一篇文章中,我只是简单地检查了进入活动的权限。 If no permission is given then I disabled the button. 如果未获得许可,则我禁用了该按钮。
If this is not practical then the other possibility is to take the code that, as you described, "is in your condition" and put it into a separate method. 如果这不切实际,那么另一种可能性就是采用您所描述的“处于您的条件中”的代码,并将其放入单独的方法中。 Then change checkForManifestPermission() to return a boolean as to whether the program already has the required permission or not. 然后更改checkForManifestPermission()以返回有关程序是否已具有所需权限的布尔值。
So, that part of the code would read something like this: 因此,该部分代码将读取如下内容:
private void methodThatNeedsPermission(){
if (checkForManifestPermission())
doTheAction();
}
private void doTheAction(){
---- your code
}
Now, in the onRequestPermissionsResult, if the user okays the permission you can then call that same method. 现在,在onRequestPermissionsResult中,如果用户同意权限,则可以调用相同的方法。 So, it becomes: 因此,它变为:
@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;
doTheAction();
} 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.