Android使用Google的位置服务对话框

时间:2016-11-21 20:52:22

标签: android location google-play-services android-geofence google-location-services

我在我的应用中使用地理围栏。如果用户在第一次设备运行后首先切换位置,他就会要求同意Google的位置。但如果用户不同意,地理围栏的init会返回错误代码1000.

有没有办法再次要求我在我的应用中使用Google位置?

我知道this questin,但此对话框仅在首次启动位置设置时启动。如果用户解除对手,有恕我直言,没有办法显示对话框。或者有什么办法吗?

谢谢你的回复

1 个答案:

答案 0 :(得分:-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;
    }