我有一个奇怪的错误让我发疯。我正在使用Android Marshmallow,我正在实施新的权限。当我单击“登录”按钮时,我会检查用户是否具有gps权限。如果没有,我请求他们。代码的工作方式是,在询问权限后,调用Async任务以从REST服务获取某些设置,然后在Async任务的onPostExecute上,它将触发Intent。
当用户允许权限时,一切正常。如果用户拒绝权限,它将调用异步任务并调用Intent,但不会激活Intent。它只是停留在屏幕上。
按钮单击
Button btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
int has_permission = 0;
if (Build.VERSION.SDK_INT >= 23)
{
has_permission = Check_Permission(Manifest.permission.ACCESS_FINE_LOCATION);
}
action = "login";
if(has_permission == 0)
{
Get_Location();
load_system_settings_async = new Load_System_Settings_Async();
load_system_settings_async.execute((Void) null);
}
}
});
检查权限代码
protected int Check_Permission(final String permission)
{
int has_permission = checkSelfPermission(permission);
if (has_permission != PackageManager.PERMISSION_GRANTED)
{
if (!shouldShowRequestPermissionRationale(permission) && (request_times > 0))
{
String title="";
if(permission.equals(Manifest.permission.ACCESS_FINE_LOCATION))
{
title = "You need to allow GPS access";
}
else if(permission.equals(Manifest.permission.WRITE_EXTERNAL_STORAGE))
{
title = "You need to allow storage access";
}
else if(permission.equals(Manifest.permission.CALL_PHONE))
{
title = "You need to allow access phone access";
}
Show_Alert_Dialog("Ok", title,
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", getPackageName(), null));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
//requestPermissions(new String[] {permission}, REQUEST_CODE_ASK_PERMISSIONS);
}
});
return has_permission;
}
requestPermissions(new String[] {permission}, REQUEST_CODE_ASK_PERMISSIONS);
request_times++;
return has_permission;
}
return has_permission;
}
权限请求
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
if (requestCode == 123)
{
if(grantResults[0] == 0)
{
Get_Location();
}
load_system_settings_async = new Load_System_Settings_Async();
load_system_settings_async.execute((Void) null);
//request_times = 0;
}
else
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
答案 0 :(得分:1)
似乎问题是当用户拒绝权限时,即使未授予权限,控制也会落入执行异步任务的onRequestPermissionsResult
。在以下代码中检查注释
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults)
{
if (requestCode == 123) // yes request code is the same go on
{
if(grantResults[0] == 0) //yes we get the approval
{
Get_Location();
}
// oh even if we didn't get the approval we still gonna execute the task
load_system_settings_async = new Load_System_Settings_Async();
load_system_settings_async.execute((Void) null);
//request_times = 0;
}
else //control only come here when request code will not match
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
你需要像这样结合if条件
if (requestCode == 123 && grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
答案 1 :(得分:0)
好吧,我想通了..当我在Intent中传递带有putExtra的自定义对象时,有一个属性为null,所以当它在对象中执行writeToParcel时,它就崩溃了。不幸的是,Android一直在执行而没有崩溃..它只是什么也没做。我只是在对象中添加了一个setter并将值设置为false。问题与权限代码无关。四个小时的生命和失眠,我不会回来。感谢所有观看者。