我必须从用户那里获得运行时权限。我在 ActivityCompat.requestPermissions 中将requestCode作为 200 传递,如下面的代码所示。
但是,当用户授予权限时,在 onRequestPermissionsResult 上, requestCode 返回 1 。返回的 requestCode 应该 200 ,对吗?
任何人都可以解释为什么requestCode在 onRequestPermissionsResult 中返回 1 而不是 200 (代码I' m pasisng to requestPermissions)用户授予/接受运行时权限后?
var mRuntimePermissions = RuntimePermissions(permissions, this)
var gotPermissions = mRuntimePermissions.hasPermissions()
if(gotPermissions == true){
startActivity(Intent(this, MainActivity::class.java))
}
else{
ActivityCompat.requestPermissions(this, permissions, 200);
}
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<String>, grantResults: IntArray) {
when (requestCode) {
// check requestCode and do something
}
}
}
答案 0 :(得分:2)
我不知道它对你有多大帮助;因为它不在
kotlin
中。但是我已经按照以下方式在许多项目中实现了它。希望它能帮助你获得至少一些线索。
班级声明:
private static String[] PERMISSIONS = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.PROCESS_OUTGOING_CALLS};
public int ALL_PERMISSIONS = 0;
public boolean CheckNoPermission = false;
现在在OnCreate:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
{
ALL_PERMISSIONS = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE);
ALL_PERMISSIONS = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.PROCESS_OUTGOING_CALLS);
// its below marshmallow
// no need to ask permissions in this case
// do the things here directly
// START YOUR ACTIVITY HERE....
}
else
{
// its marshmallow or above
// have to ask permissions in this case
// lets check if user already allowed the permissions in previous run
for(int x = 0; x < PERMISSIONS.length; x++)
{
String ThisPermission = PERMISSIONS[x];
if (checkSelfPermission(ThisPermission) != PackageManager.PERMISSION_GRANTED)
{
CheckNoPermission = true;
break;
}
}
// even if any permission is not granted then re-request all permissions;
// re-requesting all permissions will only request denied permissions only
if(CheckNoPermission)
{
// request all permissions one by one in array
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS, ALL_PERMISSIONS);
}
else
{
// user has already granted all the permissions in previous run
// do the things here directly
// START YOUR ACTIVITY HERE....
}
}
现在在onCreate:
之外检查权限结果// if permissions are requested; check for user approvals :
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length == 2)
{
Boolean IsAllPermissions = true;
// Yes need to check all permissions again as user may have denied any of the permission
for(int p = 0; p < grantResults.length; p++)
{
if (grantResults[p] != PackageManager.PERMISSION_GRANTED)
{
IsAllPermissions = false;
break;
}
}
if(IsAllPermissions)
{
// user has granted all the permissions in this run
// do the things here ...
// START YOUR ACTIVITY HERE....
}
else
{
// user has not granted all the permissions in this run
// so do what you want to do here in such case ...
}
}
else
{
// missmatch in number of permissions in result
}
}
关于onRequestPermissionsResult Is explained here...的Android文档
希望它可以帮助你或其他人
onRequestPermissionsResult
:
requestCode
不是要注意的;它的应用程序级别permissions
是此requestCode grantResults
是一个数组,其中包含了我们使用此requestCode请求的每个权限所做的任何用户requestCode无需检查,也没有任何与
有关的内容答案 1 :(得分:-1)
我们可以从startActivity
发送(执行)多个activity's
作为结果,而onActivityForResult
方法会收到多个回调,因此要确定回调是针对哪个请求,可以是使用requestCode
标识,我们可以直接将requestCode
称为请求的ID,以帮助我们处理onActivityResult
方法中针对特定请求(staryActivityForResult
)的响应。