-2

I have an android application ,I want to show GPS status permanently in my application , I used timer before to check GPS status every 3 seconds , it works correct,But I don't want to use timer now. I just want when GPS turned on , my application notify and when GPS terned off that notifiy.

1 Answers1

3

You can use the Android Broadcast Receivers mechanism.

First, define a broadcast receiver in the AndroidManifest.xml, iniside the application tag.

 <receiver android:name="com.yourpackage.GPSBroadCastReceiver">
  <intent-filter>
    <action android:name="android.location.PROVIDERS_CHANGED" />
  </intent-filter>
</receiver>

Then, create a class named GPSBroadCastReceiver for handling the broadcast message for location provider changes.

  public class GPSBroadCastReceiver  extends BroadcastReceiver
  {   
  @Override
  public void onReceive( Context context, Intent intent )
  {
      final LocationManager manager = (LocationManager) context.getSystemService( Context.LOCATION_SERVICE );
          if (manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {

            }
         else
         {

         }
  }
}

Also, you may take a look at this question.

Community
  • 1
  • 1
Farhad Faghihi
  • 11,212
  • 5
  • 30
  • 59