我想打造的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()方法内正在完成 –