【发布时间】:2017-02-20 18:28:30
【问题描述】:
我使用了以下代码,请求许可的对话按预期显示。但是当我单击“允许”时,它什么也没做。日志消息似乎没有被授予权限,所以我去我的参数验证位置是否“打开”并且它是“关闭”。它不应该是因为我授予应用程序访问我的位置的权限吗? 如果我手动将其“打开”然后再次运行该应用程序,一旦它请求我的许可,它就会工作并显示日志消息,但并不是请求许可(通过对话)打开位置(当它关闭)如果用户点击“允许”? 难道我做错了什么 ?我应该提到我正在 api23 上运行应用程序
是我的Oncreate中的代码:
mApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mApiClient.connect();
// Create the LocationRequest object
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds
这是我的 OnConnected 方法:
public void onConnected(@Nullable Bundle bundle) {
//start the service
//checking and asking for permission
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_ACCESS_FINE_LOCATION);
}
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location location = LocationServices.FusedLocationApi.getLastLocation(mApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mApiClient, mLocationRequest, this);
} else {
//If everything went fine lets get latitude and longitude
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
Log.v("currentLatitude",currentLatitude + " WORKS " + currentLongitude + "");
}
}
【问题讨论】:
-
应用权限与Location设置完全不同。有关提示用户启用位置模式的信息,请参见此处:stackoverflow.com/a/31816683/4409409
标签: android google-api geolocation android-permissions runtime-permissions