Pan*_*kaj 13 java android android-6.0-marshmallow runtime-permissions
我正在测试我的应用程序Marshmallow 6.0,它正在关闭android.permission.READ_EXTERNAL_STORAGE,即使已经在Manifest中定义了它.在某处我读过,如果我在运行时请求权限,那么它就不会强行关闭你的应用程序.我也读过这个android文档,用于请求运行时权限.
所以,我开始知道我们可以请求android文档中提到的下面的权限.
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// 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.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码有一个回调方法onRequestPermissionsResult,可以得到结果.
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是在哪里准确地向用户请求权限?我们应该在应用程序启动时使用请求权限,还是应该在需要权限时使用?
这对我有用!!! 在您的应用程序的Splash活动中执行以下操作,
1)为请求代码声明一个int变量,
private static final int REQUEST_CODE_PERMISSION = 2;
2)使用您需要的权限数声明一个字符串数组,
String[] mPermission = {Manifest.permission.READ_CONTACTS, Manifest.permission.READ_SMS,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
Run Code Online (Sandbox Code Playgroud)
3)下一步检查onCreate方法的运行时权限条件,
try {
if (ActivityCompat.checkSelfPermission(this, mPermission[0])
!= MockPackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(this, mPermission[1])
!= MockPackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(this, mPermission[2])
!= MockPackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(this, mPermission[3])
!= MockPackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
mPermission, REQUEST_CODE_PERMISSION);
// If any permission aboe not allowed by user, this condition will execute every tim, else your else part will work
}
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
4)现在声明onRequestPermissionsResult方法来检查请求代码,
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.e("Req Code", "" + requestCode);
if (requestCode == REQUEST_CODE_PERMISSION) {
if (grantResults.length == 4 &&
grantResults[0] == MockPackageManager.PERMISSION_GRANTED &&
grantResults[1] == MockPackageManager.PERMISSION_GRANTED &&
grantResults[2] == MockPackageManager.PERMISSION_GRANTED &&
grantResults[3] == MockPackageManager.PERMISSION_GRANTED) {
// Success Stuff here
}
}
}
Run Code Online (Sandbox Code Playgroud)
一般来说,在需要时立即请求所需的权限。通过这种方式,您可以告知用户为什么需要该权限并更轻松地处理权限拒绝。
考虑用户在应用程序运行时撤销权限的场景:如果您在启动时请求它并且以后不再检查它,这可能会导致意外的行为或异常。
| 归档时间: |
|
| 查看次数: |
14296 次 |
| 最近记录: |