use of com.google.android.gms.location.LocationSettingsResult in project Android-ReactiveLocation by mcharmas.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lastKnownLocationView = (TextView) findViewById(R.id.last_known_location_view);
updatableLocationView = (TextView) findViewById(R.id.updated_location_view);
addressLocationView = (TextView) findViewById(R.id.address_for_location_view);
currentActivityView = (TextView) findViewById(R.id.activity_recent_view);
locationProvider = new ReactiveLocationProvider(getApplicationContext());
lastKnownLocationObservable = locationProvider.getLastKnownLocation();
final LocationRequest locationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setNumUpdates(5).setInterval(100);
locationUpdatesObservable = locationProvider.checkLocationSettings(new LocationSettingsRequest.Builder().addLocationRequest(locationRequest).setAlwaysShow(//Refrence: http://stackoverflow.com/questions/29824408/google-play-services-locationservices-api-new-option-never
true).build()).doOnNext(new Action1<LocationSettingsResult>() {
@Override
public void call(LocationSettingsResult locationSettingsResult) {
Status status = locationSettingsResult.getStatus();
if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
try {
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException th) {
Log.e("MainActivity", "Error opening settings activity.", th);
}
}
}
}).flatMap(new Func1<LocationSettingsResult, Observable<Location>>() {
@Override
public Observable<Location> call(LocationSettingsResult locationSettingsResult) {
return locationProvider.getUpdatedLocation(locationRequest);
}
});
addressObservable = locationProvider.getUpdatedLocation(locationRequest).flatMap(new Func1<Location, Observable<List<Address>>>() {
@Override
public Observable<List<Address>> call(Location location) {
return locationProvider.getReverseGeocodeObservable(location.getLatitude(), location.getLongitude(), 1);
}
}).map(new Func1<List<Address>, Address>() {
@Override
public Address call(List<Address> addresses) {
return addresses != null && !addresses.isEmpty() ? addresses.get(0) : null;
}
}).map(new AddressToStringFunc()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
activityObservable = locationProvider.getDetectedActivity(50);
}
use of com.google.android.gms.location.LocationSettingsResult in project IITB-App by wncc.
the class MapFragment method onStart.
@Override
public void onStart() {
super.onStart();
locationButton = (FloatingActionButton) getActivity().findViewById(R.id.location_button);
locationButton.setImageResource(R.drawable.ic_my_location_black_24dp);
locationButton.getDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark), PorterDuff.Mode.SRC_IN);
locationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("MapFragment", "Location button pressed");
try {
LocationManager lm = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;
if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, MY_PERMISSIONS_REQUEST_LOCATION);
return;
}
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
if (!gps_enabled && !network_enabled) {
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);
// **************************
// this is the key ingredient
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:
// requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(getActivity(), 1000);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// settings so we won't show the dialog.
break;
}
}
});
}
currentLocation = getLastKnownLocation();
if (currentLocation != null) {
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()), 17);
googleMap.animateCamera(cameraUpdate);
locationButton.getDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.colorPrimary), PorterDuff.Mode.SRC_IN);
}
} catch (Exception e) {
checkLocationPermission();
Toast.makeText(getContext(), "Please turn on Location from the Settings", Toast.LENGTH_SHORT).show();
}
}
});
}
use of com.google.android.gms.location.LocationSettingsResult in project Thesis by bajnax.
the class LeScanActivity method enableLocation.
public void enableLocation() {
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(LeScanActivity.this).addApi(LocationServices.API).addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
googleApiClient.connect();
}
}).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("Location error", "Location error " + connectionResult.getErrorCode());
}
}).build();
googleApiClient.connect();
}
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(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch(status.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
// requesting to enable Bluetooth
status.startResolutionForResult(LeScanActivity.this, REQUEST_ENABLE_LS);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
}
}
});
}
use of com.google.android.gms.location.LocationSettingsResult in project zendrive-sdk-android-sample by zendrive.
the class MainActivity method maybeResolveErrors.
private boolean maybeResolveErrors() {
// The activity may have been launched by tapping on a notification
// for a google play settings or location permission error.
// Check and start resolution if that's the case.
Intent intent = getIntent();
if (intent == null || intent.getAction() == null) {
return false;
}
String action = intent.getAction();
boolean skipZendriveSettingsCheck = false;
switch(action) {
case Constants.EVENT_GOOGLE_PLAY_SETTING_ERROR:
LocationSettingsResult result = intent.getParcelableExtra(EVENT_GOOGLE_PLAY_SETTING_ERROR);
if (result != null && !result.getStatus().isSuccess()) {
LocationSettingsHelper.resolveLocationSettings(this, result);
}
setIntent(null);
skipZendriveSettingsCheck = true;
break;
case Constants.EVENT_LOCATION_PERMISSION_ERROR:
requestLocationPermission();
setIntent(null);
skipZendriveSettingsCheck = true;
break;
case EVENT_ACTIVITY_PERMISSION_ERROR:
requestActivityPermission();
setIntent(null);
skipZendriveSettingsCheck = true;
break;
case EVENT_MULTIPLE_PERMISSIONS_ERROR:
List<String> missingPermissionList = intent.getStringArrayListExtra(MULTIPLE_PERMISSIONS_DENIED_LIST);
if (missingPermissionList == null || missingPermissionList.isEmpty()) {
throw new RuntimeException("Cannot find missing permission list in the activity intent");
}
requestMultiplePermissions(missingPermissionList);
setIntent(null);
skipZendriveSettingsCheck = true;
break;
}
return skipZendriveSettingsCheck;
}
use of com.google.android.gms.location.LocationSettingsResult in project GmsCore by microg.
the class GoogleLocationManagerServiceImpl method requestLocationSettingsDialog.
@Override
public void requestLocationSettingsDialog(LocationSettingsRequest settingsRequest, ISettingsCallbacks callback, String packageName) throws RemoteException {
Log.d(TAG, "requestLocationSettingsDialog: " + settingsRequest);
PackageUtils.getAndCheckCallingPackage(context, packageName);
(new Handler(Looper.getMainLooper())).post(() -> {
try {
callback.onLocationSettingsResult(new LocationSettingsResult(new LocationSettingsStates(true, true, true, true, true, true), Status.SUCCESS));
} catch (RemoteException e) {
Log.w(TAG, e);
}
});
}
Aggregations