2016-10-02 56 views
0

當應用程序啓動時,將運行此代碼,請求權限。權限請求窗口在Android中拒絕權限後接受時不會關閉?

public void requestPermission() 
{ 
    ActivityCompat.requestPermissions(this, 
      new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION }, 
      Integer.parseInt(this.getString(R.string.LOCATION_REQUESTCODE))); 
} 

用戶可以接受或拒絕該權限。然後它運行下面的代碼。

@Override 
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) 
    { 
     if(requestCode == Integer.parseInt(this.getString(R.string.LOCATION_REQUESTCODE))) 
     { 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
      { 
       // permission was granted 
       // Continue running the application 
      } 
      else 
      { 
       // permission denied 
       Toast.makeText(this, R.string.gps_required, Toast.LENGTH_LONG).show(); 

       // Ask again to accept the permissions 
       requestPermission(); 
      } 
     } 
    } 

因此,如果請求被拒絕,舉杯顯示用戶需要接受它,以運行應用程序(因爲需要GPS座標)和應用程序從用戶再次請求權限。

但問題是:否決時,彈出窗口再次出現。如果我拒絕5次,那麼我想接受,在所有彈出窗口消失之前,我需要點擊接受5次。

任何想法是什麼造成這種情況?爲什麼在接受後仍然有權限彈出窗口?

回答

0

找到答案!

ActivityCompat.requestPermissions(this, 
     new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION }, 
     Integer.parseInt(this.getString(R.string.LOCATION_REQUESTCODE))); 

我在請求2個權限:android.Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION所以這就是彈出窗口出現兩次的原因。

解決方法是刪除android.Manifest.permission.ACCESS_COARSE_LOCATION,因爲Manifest.permission.ACCESS_FINE_LOCATION已包含該項。