我想打造的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()
請求權限的目的是什麼?如果回調會讓我再次要求它?我究竟做錯了什麼?我沒有從文檔中獲得太多(我知道缺乏經驗),並想知道如何解決這個問題。任何指針和代碼建議將不勝感激!
「我得到的紅線告訴我我需要提出許可請求「 - 您將得到紅色的低標號,表示IDE在您擁有運行時權限時無法確定您是否僅執行此代碼。 IDE並不是一個全能的無所不知的神,這是很好的,因爲一個馬車神可能會*非常*有問題。 「想知道如何解決這個問題」 - 暫時忽略它。運行你的代碼。一旦你的代碼在這個區域工作,使用快速修復添加'@ SuppressLint'註釋來擺脫紅色低調。 – CommonsWare
你在哪裏調用'mGoogleApiClient.connect();'..post完整代碼.. – rafsanahmad007
@ rafsanahmad007 onStart()方法內正在完成 –