I want to implement Enable/Disable mobile data and GPS settings
.
I have searched for available Android API of enable/disable mobile data and GPS settings and below are my findings.
Enable/Disable Mobile data –
1. It is possible to perform enabling/disabling in <5.0 Android OS.
2. From 5.0+ Android OS, It is not yet possible for a non-system app to enable/disable mobile data.
From 5.0 or above we get this exception while doing same – Which is Not for use by third-party applications.
Caused by: java.lang.SecurityException:
Neither user 10314 nor current process has android.permission.MODIFY_PHONE_STATE
.
Is there any possible/available solutions available?
GPS Settings - 1. We are able to ON location programmatically but not yet found a way to disable it(excluding Intent approach).
Anyone know how to disable GPS(Location programmatically) without moving to settings via intent.
Thanks in Advance.
We are changing the GPS setting without moving to setting screen using SettingsApi
To check whether GPS is ON or OFF, you have to check like below,
public class GPSActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static String TAG = "GPSActivity";
// Required for setting API
protected static final int REQUEST_CHECK_SETTINGS = 0x1;
GoogleApiClient googleApiClient;
private Button mBtnGPS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps);
mBtnGPS = (Button) findViewById(R.id.btnGPS);
mBtnGPS.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnGPS:
// Check GPS
checkGps();
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
if (requestCode == REQUEST_CHECK_SETTINGS) {
googleApiClient = null;
checkGps();
}
}
public void checkGps() {
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(GPSActivity.this)
.addApiIfAvailable(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:
Log.i("GPS", "SUCCESS");
//getFbLogin();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i("GPS", "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(GPSActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i("GPS", "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;
case LocationSettingsStatusCodes.CANCELED:
Log.i("GPS", "CANCELED");
break;
}
}
});
}
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
Here is the Documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With