15

GPS ベースのアプリケーションでは、ユーザーが GPS を有効にすることが重要です。そうでない場合は、通常、ユーザーが「この機能を使用できるように設定から GPS を有効にする必要がある」ことを示すダイアログを表示します。

ユーザーが [OK] を押すと、[設定] ページにリダイレクトされます。ユーザーがアプリケーション コンテキストから設定に移動するため、このソリューションは好きではありません。

「google maps」アプリケーションには、GPS 機能が必要な場合にきちんとしたダイアログを表示するというより良い解決策があることに気付きました。ユーザーが「OK」を選択すると、設定にリダイレクトされることなく、 GPS が直接有効になります。

「google maps」アプリのようにユーザーを設定画面にリダイレクトせずに GPS を有効にすることはできますか?

以下の画像をチェックしてください。

ニートダイアログ

4

1 に答える 1

22

その機能を使用するには、次のものが必要です。

  • 最初に (少なくとも) プレイ サービスのバージョン 7.0

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

  • コードで次のようなものを2番目に使用します(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);

次に、コールバックを作成します。

// 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;
        }
    }
};

そして、最後に、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;
    }
}
于 2015-04-20T10:00:12.887 に答える