Detecting GPS on/off switch in Android phones
As I have found out the best way to do this is to attach to the
<action android:name="android.location.PROVIDERS_CHANGED" />
intent.
For instance:
<receiver android:name=".gps.GpsLocationReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
And then in the code:
public class GpsLocationReceiver extends BroadcastReceiver implements LocationListener
...
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED"))
{
// react on GPS provider change action
}
}
How can I detect if user disable GPS (Android - Play Services)
You can achive this by Setting up a Broadcast Receiver that gets fired whenever the GPS of your device turns on/off
Steps:
1. First of all create a class GPS which extend BroadcastReceiver
public class GPSCheck extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
LocationManager locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
}
else
{
Toast.makeText(context, "Please switch on the GPS", Toast.LENGTH_LONG).show();
}
}
}
2. And then mention this class in your AndroidManifest.xml file
<receiver android:name="com.yourpackagename.example.GPSCheck" >
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
3. Do not forget to add this permission also
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
How to trigger broadcast receiver when gps is turn on/off?
This is useful when user want to trigger any action on turn On/Off location provides
You should add this action in manifest
<action android:name="android.location.PROVIDERS_CHANGED" />
and after add this action you can trigger your broadcast receiver
<receiver android:name=".GpsLocationReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
And in your BroadcastReceiver
class you have to implement LocationListener
in that class which is given following below..
public class GpsLocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
Toast.makeText(context, "in android.location.PROVIDERS_CHANGED",
Toast.LENGTH_SHORT).show();
Intent pushIntent = new Intent(context, LocalService.class);
context.startService(pushIntent);
}
}
}
Related Topics
"This App Is Not Authorized to Use Firebase Authentication" in Emulator
How to Change Actionmode Background Color in Android
Xml Rendering Errors Android Preview N
Android: Outofmemoryerror: Bitmap Size Exceeds Vm Budget with No Reason I Can See
How to Use Tabhost.Ontabchangelistener in Android
Flutter - How to Pass Custom Arguments in Firebase Dynamic Links for App Invite Feature
Open a Selected File (Image, PDF, ...) Programmatically from My Android Application
How to Know If a Fragment Is Visible
How to Set Text of Text View in Another Thread
Mediaplayer.Setdatasource() and Prepare() Not Working - Android
Android Error - Caused By: Java.Lang.Noclassdeffounderror: Android.Support.V4.Util.Sparsearraycompat
Firebase Authentication State Change Does Not Fire When User Is Disabled or Deleted
Cannot Deserialize Instance of Object Out of Start_Array Token in Spring Webservice
How to Change Color of the Toggle Button
Push Up Content When Clicking in Edit Text
After Update of as to 1.0, Getting "Method Id Not in [0, 0Xffff]: 65536" Error in Project