Last active
June 25, 2020 10:26
-
-
Save irfaan008/f977c043ff7dfe3f71ce979a2ee52ebe to your computer and use it in GitHub Desktop.
Enable Gps automatically if it is turned off. Similar to what OLA cab does. However to comply with policy it shows a popup to user with OK and CANCEL button. Still its better than showing location setting screen to user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.irinnovative.enablegps; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.content.IntentSender; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.common.api.PendingResult; | |
import com.google.android.gms.common.api.ResultCallback; | |
import com.google.android.gms.common.api.Status; | |
import com.google.android.gms.location.LocationRequest; | |
import com.google.android.gms.location.LocationServices; | |
import com.google.android.gms.location.LocationSettingsRequest; | |
import com.google.android.gms.location.LocationSettingsResult; | |
import com.google.android.gms.location.LocationSettingsStates; | |
import com.google.android.gms.location.LocationSettingsStatusCodes; | |
import butterknife.ButterKnife; | |
import butterknife.OnClick; | |
public class TestActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_test); | |
// Bind your views manually here | |
ButterKnife.bind(this); | |
} | |
@OnClick(R.id.btnClick) | |
public void onClick() { | |
EnableGPSAutoMatically(); | |
} | |
private void EnableGPSAutoMatically() { | |
GoogleApiClient googleApiClient = null; | |
if (googleApiClient == null) { | |
googleApiClient = new GoogleApiClient.Builder(this) | |
.addApi(LocationServices.API).addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this).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); // this is the key ingredient | |
// ************************** | |
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(); | |
final LocationSettingsStates state = result | |
.getLocationSettingsStates(); | |
switch (status.getStatusCode()) { | |
case LocationSettingsStatusCodes.SUCCESS: | |
toast("Success"); | |
// All location settings are satisfied. The client can | |
// initialize location | |
// requests here. | |
break; | |
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: | |
toast("GPS is not on"); | |
// 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(TestActivity.this, 1000); | |
} catch (IntentSender.SendIntentException e) { | |
// Ignore the error. | |
} | |
break; | |
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: | |
toast("Setting change not allowed"); | |
// Location settings are not satisfied. However, we have | |
// no way to fix the | |
// settings so we won't show the dialog. | |
break; | |
} | |
} | |
}); | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == 1000) { | |
if(resultCode == Activity.RESULT_OK){ | |
String result=data.getStringExtra("result"); | |
} | |
if (resultCode == Activity.RESULT_CANCELED) { | |
//Write your code if there's no result | |
} | |
} | |
} | |
@Override | |
public void onConnected(@Nullable Bundle bundle) { | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
toast("Suspended"); | |
} | |
@Override | |
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { | |
toast("Failed"); | |
} | |
private void toast(String message) { | |
try { | |
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); | |
} catch (Exception ex) { | |
log("Window has been closed"); | |
} | |
} | |
} |
can we do this in background
Hi yes, I did this with a good way.
After turn on GPS, how can I trun off programatically using same code?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can this be done via fragment call and getActivity Result in fragment?