【问题标题】:Android Use Google's location service dialogAndroid 使用 Google 的位置服务对话框
【发布时间】:2017-04-05 09:04:43
【问题描述】:
我在我的应用程序中使用地理围栏。如果用户在第一次设备运行后第一次切换位置,我们会要求他同意谷歌位置。但是如果用户不同意,地理围栏的初始化会返回错误代码 1000。
有什么方法可以再次要求我在我的应用中使用 Google 位置信息吗?
我知道this questin,但此对话框仅在首次启动位置设置时启动。如果用户拒绝,恕我直言,无法显示对话框。或者有什么办法吗?
谢谢你的回答
【问题讨论】:
标签:
android
location
google-play-services
android-geofence
google-location-services
【解决方案1】:
正如您在问题中所评论的那样,我不确定您在问什么,但您可以使用shouldShowRequestPermissionRationale 测试用户最初是否不同意位置权限。
下面是一些基于我使用的Requestion Permissions 文档的扩展代码,您可能会发现这些代码对您有所帮助。
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "checkMyPermissions ( " + permissionRequestCode + ") : Permissions FAILED");
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
// 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.
new AlertDialog.Builder(this)
.setMessage("In order to show data this app requires location permissions")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
permissionRequestCode);
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
permissionRequestCode);
}
return false;
} else {
Log.d(TAG, "checkMyPermissions ( " + permissionRequestCode + ") : Permissions Passed");
return true;
}