Mobile data and GPS ON / OFF programmatically - Android above 5.0 too

I want to implement Enable/Disable mobile data and GPS settings . I was looking for an affordable Android API to turn on / off mobile data and GPS settings, and below are my results.

Turn on / off mobile data - 1. You can enable / disable on <5.0 Android OS. 2. From 5.0+ Android OS, it is not yet possible to use a non-system application to enable / disable mobile data. From 5.0 or higher, we get this exception by doing the same thing - which is not used by third-party applications. Called: java.lang.SecurityException: Neither user 10314 nor current process has android.permission.MODIFY_PHONE_STATE .

Are there any affordable / affordable solutions?

GPS settings - 1. We can programmatically determine ON, but have not yet found a way to disable it (excluding the Intent approach).

Does anyone know how to disable GPS (location programmatically) without going into settings using intent.

Thanks at Advance.

+5
source share
2 answers

We change the GPS setting without going to the settings screen using the SettingsApi

To check if GPS is on or off, you should check as 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

+1
source

Here's how to get started with a location service from GPS and network providers (Wi-Fi / data).

 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 to location updates, run this code: locationManager.removeUpdates(locationListener);

One line of code stops listening to any location updates regardless of the provider (GPS / Network), because the LocationManager does not care about where the updates come from.

In your case, I suppose you know how to create some kind of user interface 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 has enabled both providers, location updates may be more accurate. And if the user has disabled both providers, it does not matter. Since there will be no updates in the LocationListener, this should be what the user wanted.

By the way, here is the code to create a 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) { } }; 
0
source

Source: https://habr.com/ru/post/1260503/


All Articles