我有一個奇怪的錯誤讓我發瘋。我正在開發Android Marshmallow,並且正在實施新的權限。當我點擊我的登錄按鈕時,我檢查用戶是否擁有gps權限。如果不是,我請求他們。代碼工作的方式是,在詢問權限後,調用異步任務以從REST服務獲取一些設置,然後在異步任務的onPostExecute上,它將激發一個Intent。Android onRequestPermissionsResult工作不正常
當用戶允許權限時,一切正常。如果用戶拒絕權限,它將調用Async任務並調用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);
}
}
我想執行異步,無論拒絕或允許的權限。如果我添加你的代碼,它只會在允許的情況下執行。如果我一步一步通過我的代碼,它似乎工作正常..除非我的意圖內的異步不會被調用。順便說一句,我沒有嘗試你的代碼,它根本不運行我的異步,這不是我想要的。 – SurfingViolinist
我明白了,那麼我需要看看你的異步任務 –