2016-08-05 170 views
0

我有一個奇怪的錯誤讓我發瘋。我正在開發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); 
    } 
} 

回答

1

好像問題是,當用戶在您正在執行的非同步任務,即使不被許可在以下代碼

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); 
    } 
} 

你需要的,如果要結合。檢查意見否認權限,控制下跌onRequestPermissionsResult這樣

if (requestCode == 123 && grantResults.length > 0 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
+0

我想執行異步,無論拒絕或允許的權限。如果我添加你的代碼,它只會在允許的情況下執行。如果我一步一步通過我的代碼,它似乎工作正常..除非我的意圖內的異步不會被調用。順便說一句,我沒有嘗試你的代碼,它根本不運行我的異步,這不是我想要的。 – SurfingViolinist

+0

我明白了,那麼我需要看看你的異步任務 –

0

好的條件,我想通了。當我在意圖傳遞一個自定義對象與putExtra,有那是空的屬性,所以當它在執行對象writeToParcel,它正在崩潰。不幸的是,Android一直在執行,沒有崩潰..它只是沒有做任何事情。我只是將一個setter添加到該對象並將該值設置爲false。問題與權限代碼無關。四個小時的生活和失眠,我不會回來。感謝所有查看的人。