【发布时间】:2017-09-13 06:24:38
【问题描述】:
我正在尝试构建一个关于 Android N 中权限请求过程的简单示例。
我已经在我的清单文件中声明了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注释以消除红色下划线。 -
你在哪里打电话
mGoogleApiClient.connect();..发布完整代码.. -
@rafsanahmad007 正在 onStart() 方法中完成一项
标签: java android android-permissions android-location