I am building an application in which I have to constantly save user\'s location and then send it to the server. For that, I am using FusedLocationApis in a service. For the
You can use location state changed receiver
to know when user manually turns of location
GpsReceiver.java
public class GpsReceiver extends BroadcastReceiver {
private final LocationCallBack locationCallBack;
/**
* initializes receiver with callback
* @param iLocationCallBack Location callback
*/
public GpsReceiver(LocationCallBack iLocationCallBack){
this.locationCallBack = iLocationCallBack;
}
/**
* triggers on receiving external broadcast
* @param context Context
* @param intent Intent
*/
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
locationCallBack.onLocationTriggered();
}
}
}
Create an interface
to communicate GPS changes to Activity
public interface LocationCallBack {
/**
* on Location switch triggered
*/
void onLocationTriggered();
}
Register receiver
in onCreate()
of Activity
to start listening to GPS state changes
public void onCreate(Bundle savedInstance){
//---
registerReceiver(new GpsReceiver(new LocationCallBack() {
@Override
public void onLocationTriggered() {
//Location state changed
}
}), new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
//---
}
This code worked on Android 9 (Pie) version perfectly
public class MainActivity extends AppCompatActivity {
private MyLocationReceiver mLocationReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationReceiver = new MyLocationReceiver(this, Snackbar.make(findViewById(android.R.id.content), "Location service is not enabled", Snackbar.LENGTH_INDEFINITE));
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(mLocationReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
}
@Override
protected void onPause() {
super.onPause();
try {
unregisterReceiver(mLocationReceiver);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class MyLocationReceiver extends BroadcastReceiver {
private static final String TAG = "MyLocationReceiver";
private Context context;
private Snackbar snackbar;
public MyLocationReceiver(Context context, Snackbar snackbar){
this.context = context;
this.snackbar = snackbar;
}
@Override
public void onReceive(Context context, Intent intent) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(gpsEnabled && networkEnabled) {
if (snackbar != null) {
snackbar.dismiss();
}
Log.d(TAG, "GPS is enabled");
} else {
snackbar.show();
Log.d(TAG, "GPS is disabled");
}
}
}
}
You can use below to check Location availability:
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
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){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage(getResources().getString(R.string.gps_network_not_enabled));
dialog.setPositiveButton(getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
Startup.this.startActivity(myIntent);
}
});
dialog.setNegativeButton(getString(R.string.Cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
}
});
dialog.show();
}