Reputation: 2336
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.
Upvotes: 5
Views: 933
Reputation: 71
This is how to start the location service, from GPS and Network(Wi-Fi/data) providers.
LocationManager locationManager = (LocationManager)getContext().getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
If you want to stop listening for location updates, run this code:
locationManager.removeUpdates(locationListener);
The single line of code would stop listening for any location updates, regardless of the provider(GPS/Network), because the LocationManager doesn't care where the updates come from.
In your case, I assume you know how to create some UI to let the user decide whether to use GPS/Network or not. And then, you can do something like this:
if ( useGPS ) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
}
if ( useNetwork ) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
}
If the user enabled both providers, the location updates could be more accurate. And if the user disabled both providers, it doesn't matter. Since no updates will come to the LocationListener, and it should be what the user wanted.
By the way, here's the code to create the LocationListener:
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// you may add some logic here to determine whether the new location update is more accurate than the previous one
if ( isBetterLocation(location,currentBestLocation) ) {
currentBestLocation = location;
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
Upvotes: 0
Reputation: 667
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
Upvotes: 1