【问题标题】:Turn on gps in android with user's permission directly from the android app直接从android应用程序在用户许可的情况下在android中打开gps
【发布时间】:2015-07-18 08:27:25
【问题描述】:

我是安卓新手。我正在构建一个应用程序,如果 gps 打开,则通过 gps 捕获位置,如果 gps 关闭,则由网络提供商捕获位置。现在我想要的是,当用户打开应用程序时,应该会弹出一条消息,表明应用程序想要打开 gps 以提高准确性,就像在 ola cab 应用程序中一样......用户有两个选项,是或否.... 如果用户单击是,则 gps 将打开。我该怎么做??

【问题讨论】:

  • 问题是什么?

标签: android


【解决方案1】:

Google Play Services 7 引入了一种直接从应用打开 GPS 的方法。 有关更多信息,请参阅来自 google 的this sample。具体代码块是

    protected void checkLocationSettings() {
    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(
                    mGoogleApiClient,
                    mLocationSettingsRequest
            );
    result.setResultCallback(this);
}

/**
 * The callback invoked when
 * {@link com.google.android.gms.location.SettingsApi#checkLocationSettings(GoogleApiClient,
 * LocationSettingsRequest)} is called. Examines the
 * {@link com.google.android.gms.location.LocationSettingsResult} object and determines if
 * location settings are adequate. If they are not, begins the process of presenting a location
 * settings dialog to the user.
 */
@Override
public void onResult(LocationSettingsResult locationSettingsResult) {
    final Status status = locationSettingsResult.getStatus();
    switch (status.getStatusCode()) {
        case LocationSettingsStatusCodes.SUCCESS:
            Log.i(TAG, "All location settings are satisfied.");
            startLocationUpdates();
            break;
        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
            Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to" +
                    "upgrade location settings ");

            try {
                // Show the dialog by calling startResolutionForResult(), and check the result
                // in onActivityResult().
                status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
            } catch (IntentSender.SendIntentException e) {
                Log.i(TAG, "PendingIntent unable to execute request.");
            }
            break;
        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
            Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
                    "not created.");
            break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        // Check for the integer request code originally supplied to startResolutionForResult().
        case REQUEST_CHECK_SETTINGS:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    Log.i(TAG, "User agreed to make required location settings changes.");
                    startLocationUpdates();
                    break;
                case Activity.RESULT_CANCELED:
                    Log.i(TAG, "User chose not to make required location settings changes.");
                    break;
            }
            break;
    }
}

Credit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    相关资源
    最近更新 更多