Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

android - Gps status enabled/disabled broadcast receiver

I m trying to register a broadcast receiver to receive updates when gps status is changed.

However, my GpsChangeReceiver onReceive method doesn't seem to be called when gps status is changed from enabled to disabled or vice-versa.

First of all, i'm registering the reciever:

GpsChangeReceiver m_gpsChangeReceiver = new GpsChangeReceiver();
this.registerReceiver(m_gpsChangeReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));

Then, I have my GPS Receiver

public class GpsChangeReceiver 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 ) ) {
        //do something
    }
    else
    {
        //do something else
    }
  }
}

Finally, my manifest contains:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Can anyone see why the gps receiver is never called when I modify its status from the phone's settings??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two ways of registering broadcast receivers in Android:

  1. in the code (your case)
  2. in AndroidManifest.xml

Let me explain the differences.

Case 1 (Broadcast Receiver registered in the code)

You will only receive broadcasts as long as context where you registered your receiver is alive. So when activity or application is killed (depending where you registered your receiver) you won't receive broadcasts anymore.

I guess you registered broadcast receiver in the activity context which is not very good approach. If you want to register broadcast receiver for your application context you can do something like this:

getApplicationContext().registerReceiver(m_gpsChangeReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));

Case 2 (Broadcast Receiver registered in AndroidManifest.xml)

You will receive broadcasts even when your application is killed (system will wake your application up). This is right approach when you want to receive broadcasts no matter if your app is running.

Add this receiver to your AndroidManifest.xml:

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

EDIT: Some special broadcasts (i.e. SCREEN_ON or SCREEN_OFF) must be registered in code (case 1) otherwise they won't be delivered to your application.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...