21

これを尋ねる理由: (アプリで試す理由でもあります)

Lollipop で Google マップを使用すると発生します。場所が無効になっている場合でも、ユーザーがマップ アプリから入力した後、[設定] にアクセスしなくても、高精度モードで有効になります。

Bluetooth を有効にするために同様の機能を実現できます。この場合、アプリでアクションが開始されます。ユーザーは選択を行う必要がありますが、ユーザーは以下を使用して設定にリダイレクトされません。

startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));

BluetoothAdapterで見つけることができたので、 LocationAdapter がないことがわかったので、gms->LocationServicesを調べました。基本的にLocation API 参照android.location.LocationManagerの下のほとんどすべてですが、まだ利用できるようなものはないようですACTION_REQUEST_ENABLE

同じ方法が他にもあることを願っており、より多くの人がそれを試しています。

注意してください:
context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));そのようには機能しません。

4

3 に答える 3

22

上記の @ user2450263 のメモに基づいて、ここにいくつかのスニペットがあります。

/**
 * Prompt user to enable GPS and Location Services
 * @param mGoogleApiClient
 * @param activity
 */
public static void locationChecker(GoogleApiClient mGoogleApiClient, final Activity activity) {
    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(30 * 1000);
    locationRequest.setFastestInterval(5 * 1000);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest);
    builder.setAlwaysShow(true);
    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            final LocationSettingsStates state = 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(
                                activity, 1000);
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way to fix the
                    // settings so we won't show the dialog.
                    break;
            }
        }
    });
}

そして、それを次のように使用します。

mGoogleApiClient = new GoogleApiClient
            .Builder(this)
            .enableAutoManage(this, 34992, this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    locationChecker(mGoogleApiClient, MyActivity.this);

基本的に、ユーザーはこのようなプロンプトを受け取り、設定に移動せずに高精度モードで位置情報を有効にできます GPS をオンにする

于 2015-10-29T08:03:22.867 に答える