2016年10月14日 星期五

Location Setting

Ref :  https://github.com/googlesamples/android-play-location/tree/master/LocationSettings



按下按鈕  

 startUpdatesButtonHandler() -> checkLocationSettings();


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


當點選選項後... 會呼叫    onResult()




@Overridepublic 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;
    }
}



一開始的時候...會先得到  LocationSettingsStatusCodes.RESOLUTION_REQUIRED:

會呼叫這個.....
   status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);


再按一次Button....

這時候就會得到 LocationSettingsStatusCodes.SUCCESS:

然後就可以開始執行  location update





沒有留言:

張貼留言