简体   繁体   English

Android:我可以在不将用户重定向到“谷歌地图”应用程序中的设置屏幕的情况下启用 GPS

[英]Android: Can I enable the GPS without redirecting the user to the settings screen like in "google maps" app

In GPS based applications, it is important that the user enable his GPS.在基于 GPS 的应用程序中,用户启用他的 GPS 很重要。 If not then usually we would show a dialog stating that the user " should enable his GPS from the settings to be able to use this functionality ".如果没有,那么通常我们会显示一个对话框,说明用户“应该从设置中启用他的 GPS 才能使用此功能”。

When the user press OK he will be redirected to the Settings page, I don't like this solution since it takes the user out of the application context in to the settings.当用户按 OK 时,他将被重定向到“设置”页面,我不喜欢这个解决方案,因为它将用户从应用程序上下文中带到设置中。

I have noticed that "google maps" application has a better solution, which is to show a neat dialog when a GPS feature is needed.我注意到“谷歌地图”应用程序有一个更好的解决方案,即在需要 GPS 功能时显示一个简洁的对话框。 Upon the user's selection "OK" GPS will be enabled directly without any redirection to the settings.一旦用户选择“OK” GPS将直接没有任何重定向到设置中启用。

Can I enable the GPS without redirecting the user to the settings screen like in "google maps" app?我可以在不将用户重定向到“谷歌地图”应用程序中的设置屏幕的情况下启用 GPS 吗?

checkout the image below:结帐下面的图片:

整洁的对话框

To have that feature you need:要拥有该功能,您需要:

  • First (at least) the version 7.0 of play services首先(至少)7.0版的播放服务

compile 'com.google.android.gms:play-services-location:16.0.0'

  • Second something like this in your code (I had it in my onCreate):在你的代码中第二个类似的东西(我在我的 onCreate 中有它):

- ——

 // Check the location settings of the user and create the callback to react to the different possibilities
LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder()
                .addLocationRequest(mLocationRequest);
PendingResult<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, locationSettingsRequestBuilder.build());
result.setResultCallback(mResultCallbackFromSettings);

And then create the callback:然后创建回调:

// The callback for the management of the user settings regarding location
private ResultCallback<LocationSettingsResult> mResultCallbackFromSettings = new ResultCallback<LocationSettingsResult>() {
    @Override
    public void onResult(LocationSettingsResult result) {
        final Status status = result.getStatus();
        //final LocationSettingsStates locationSettingsStates = result.getLocationSettingsStates();
        switch (status.getStatusCode()) {
            case LocationSettingsStatusCodes.SUCCESS:
                // All location settings are satisfied. The client can initialize location
                // requests here.
                break;
            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                // Location settings are not satisfied. But could be fixed by showing the user
                // a dialog.
                try {
                    // Show the dialog by calling startResolutionForResult(),
                    // and check the result in onActivityResult().
                    status.startResolutionForResult(
                            MapActivity.this,
                            REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException e) {
                    // Ignore the error.
                }
                break;
            case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                Log.e(TAG, "Settings change unavailable. We have no way to fix the settings so we won't show the dialog.");
                break;
        }
    }
};

And then, finally, in onActivityResult I had the following:然后,最后,在onActivityResult我有以下内容:

/**
 * Used to check the result of the check of the user location settings
 *
 * @param requestCode code of the request made
 * @param resultCode code of the result of that request
 * @param intent intent with further information
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    //final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent);
    switch (requestCode) {
        case REQUEST_CHECK_SETTINGS:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    // All required changes were successfully made
                    if (mGoogleApiClient.isConnected() && userMarker == null) {
                        startLocationUpdates();
                    }
                    break;
                case Activity.RESULT_CANCELED:
                    // The user was asked to change settings, but chose not to
                    break;
                default:
                    break;
            }
            break;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我向用户显示了启用gps设置的提示,但是如何检查用户是否在手机上真正启用了gps位置? - i showed prompt to user to enable gps settings,but how can i check user really enabled the gps location on his phone? 以编程方式启用GPS(无需导航到位置设置) - Enable GPS programmatically Android (without navigating to the location settings) 要求用户在没有 GoogleAPI 或 Google Play 服务的情况下在 android 中启用 GPS 定位 - Ask user to enable GPS location in android without GoogleAPI or Google Play Services 如何确保我的应用程序可以在没有互联网连接的情况下与外部库(例如Google Maps)一起使用? - How can I ensure that my app works with external libraries like Google Maps without an internet connection? 如何像在 Google 地图应用中一样将 Android 工具栏菜单/图标向左对齐? - How can I align Android Toolbar menu/icons to the left like in Google Maps app? 谷歌地图上的当前位置而不打开 GPS(Android 设备)? - current location on google maps without turning GPS on (android device)? 如何在不使用GPS的情况下获取A用户的地理坐标并将其显示在Google Maps Activity上? - How Can I Obtain A users geographic coordinates without USing GPS and display It on A Google Maps Activity? 黑莓 - 使用GPS和谷歌地图评估Android应用程序的端口 - Blackberry - Estimate port of Android app with GPS and Google Maps GPS在Google地图上比我的App android更有效 - GPS works better with google maps than my App android 如果使用Google Maps的Android应用程序上的GPS关闭,该怎么办 - What to do if GPS is off on a Android App with Google Maps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM