Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting GPS on/off switch in Android phones

Tags:

android

gps

I would like to detect when the user changes the GPS settings on or off for an Android phone. Meaning when user switches GPS sattelite on/off or detection via access points etc.

like image 481
Drejc Avatar asked Jun 16 '11 07:06

Drejc


People also ask

How do I know if my Android GPS is on or off?

Add a TextView and a Button in the layout file. The TextView will display if the GPS is enabled or disabled upon the Button trigger.

How do I turn on GPS automatically on Android?

Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.

Why is my phone GPS disabled?

To minimize battery usage, the Android system automatically disables some features such as the GPS when the power is low or battery saving mode is turned on. To be able to use your GPS effectively again, turning off battery saver could help you out.


1 Answers

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          } } 
like image 178
Drejc Avatar answered Oct 04 '22 12:10

Drejc