2017-04-17 53 views
0

我想打造的Android N.位置權限再次onRequestPermissionsResult

我已經宣佈了ACCESS_COARSE_LOCATION使用我的清單文件中使用有關的權限請求處理一個簡單的例子。而且我想自己處理方法中的要求:當應用程序連接

public boolean permissionsChecker() { 
     // Here, thisActivity is the current activity 
     if (ContextCompat.checkSelfPermission(MainActivity.this, 
       Manifest.permission.ACCESS_COARSE_LOCATION) 
       != PackageManager.PERMISSION_GRANTED) { 

      // Should we show an explanation? 
      if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, 
        Manifest.permission.READ_CONTACTS)) { 

       // Show an explanation to the user *asynchronously* -- don't block 
       // this thread waiting for the user's response! After the user 
       // sees the explanation, try again to request the permission. 

      } else { 

       // No explanation needed, we can request the permission. 

       ActivityCompat.requestPermissions(MainActivity.this, 
         new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 
         MY_REQUEST_PERMISSION_COARSE_LOCATION); 
       return true; 

       // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
       // app-defined int constant. The callback method gets the 
       // result of the request. 
      } 
     } 
     return false; 

    } 

這得到第一問:

@Override 
    public void onConnected(@Nullable Bundle bundle) { 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      if(permissionsChecker()){ 
       mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
      } 
     } 
     if(mLastLocation != null){ 
      Log.i(TAG, mLastLocation.getLatitude() + ""); 
      Log.i(TAG, mLastLocation.getLongitude() + ""); 
     } else { 
      Log.i(TAG, "Still awaiting permissions, something went wrong"); 
     } 

    } 

我的問題是,它仍然爲mLastLocation實例返回null和我能夠在調試器中看到它,根據文檔,我必須在onRequestPermissionsResult()回調中有一個請求代碼時設置所有內容,但是當我嘗試在其中設置mLastLocation實例時,我會看到紅線告訴我我需要提出許可請求:

@Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     switch (requestCode){ 
      case MY_REQUEST_PERMISSION_COARSE_LOCATION: { 
       if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ 
        // permissiosn was granted yaaaay! Do the coarse location related task you need to do 
        //Log.i(TAG, mLastLocation.getLongitude() + ""); 
        //Log.i(TAG, mLastLocation.getLatitude() + ""); 
        //mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
       } else { 
        // permissions denied, boo! Disable the functionality that depends on this permissions 
       } 
       return; 

       // Still inside the case checker, perform other permissions that this app might require working on 
      } // end of case test for the acces coarse location 
     } // end of stich statmeent 
    } // end of onRequestPermissionsResult() 

請求權限的目的是什麼?如果回調會讓我再次要求它?我究竟做錯了什麼?我沒有從文檔中獲得太多(我知道缺乏經驗),並想知道如何解決這個問題。任何指針和代碼建議將不勝感激!

+0

「我得到的紅線告訴我我需要提出許可請求「 - 您將得到紅色的低標號,表示IDE在您擁有運行時權限時無法確定您是否僅執行此代碼。 IDE並不是一個全能的無所不知的神,這是很好的,因爲一個馬車神可能會*非常*有問題。 「想知道如何解決這個問題」 - 暫時忽略它。運行你的代碼。一旦你的代碼在這個區域工作,使用快速修復添加'@ SuppressLint'註釋來擺脫紅色低調。 – CommonsWare

+0

你在哪裏調用'mGoogleApiClient.connect();'..post完整代碼.. – rafsanahmad007

+0

@ rafsanahmad007 onStart()方法內正在完成 –

回答

1

permissionChecker()用法看起來像是在扔東西。僅僅因爲你已經申請了許可並不意味着它已經被授予。

這Droicon通話將有助於解釋的權限模型及用法:https://youtu.be/WGz-alwVh8A

您也可能會發現這個庫讓你的代碼更簡單,更容易跟隨有所幫助:https://github.com/hiqes/andele

+0

用戶點擊'允許'按鈕不會意味着許可已被授予? –

+0

是的,但是直到調用'onRequestPermissionResult()'來通知您它已被授予時,您的應用纔會知道發生了這種情況。 –

+0

糾正我,如果我錯了。至於我從文檔中瞭解的內容,當你需要請求特定權限時(比如permissionsChecker()),permissionsChecker然後顯示對話框,用戶接受它,然後將結果傳遞給回調函數,之後如果權限是由用戶提供的,它可以讓你做任何你想做的事情。我的觀點是,在那一點上,它仍然不會讓我實例化這個位置,我不知道爲什麼。感謝您的github回購鏈接!它看起來很有用,我會盡快研究它。 –