当我试图请求许可时,请求对话框出现,背景变黑,无论用户按下什么"允许"或"拒绝"应用程序正在关闭,而不是像文档中提到的那样重新启动。这是我的代码
if (ActivityCompat.checkSelfPermission(Login_Activity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(Login_Activity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(Login_Activity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults)
{
switch (requestCode)
{
case 1:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
//
}
else
{
//
}
break;
}
}
答案 0 :(得分:1)
活动不会重启。一旦用户授予权限,您应该在此处调用所需的功能。
用户响应后,将调用此方法。
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults)
{
switch (requestCode)
{
case 1:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
// Here you should call the functions whatever you want
}
else
{
//If user didn't provide you permission. You will get this else statement. SO you should omit to call the functions(ex. If you requested for the camera access . You shouldn't call the camera functions after user denying permission. If you do app will crash)
}
break;
}
}
如果您想在用户授予权限后重新启动课程
Intent intent = getIntent();
finish();
startActivity(intent);
在if语句中调用此函数。