How do I receive the system broadcast when GPS status has changed?

后端 未结 1 943
北海茫月
北海茫月 2020-12-15 02:11

I wrote the code below, but it\'s not working. Can anybody help me? I just want to passively receiving GPS status changes, rather than proactive inquiries. Saving power is m

相关标签:
1条回答
  • 2020-12-15 02:32

    I believe you are simply pointing to the wrong location in the manifest, change the receiver's name:

    <receiver android:name=".GPStStatusReceiver">
    

    This type of receiver is great for starting an app whenever the GPS is enabled, but while the app is running the LocationListener's onProviderEnabled() or onProviderDisable() will catch these even if the refresh interval is set to 10 days and a 1000 miles, or more. So you will not necessarily waste battery power if you pass generous settings to the requestLocationUpdates() method.

    Addition from comments

    I can only guess that you are not receiving the GPS_ENABLED_CHANGE because you are not triggering a location request. Simply enabling the GPS feature by clicking the checkbox in the Locations menu will not broadcast this Intent, some app must request the current location. Also, I didn't find any official documentation on this Intent which means Android might change or remove this at any time.

    Perhaps you want the officially supported LocationManager.PROVIDERS_CHANGED_ACTION, this will broadcast an Intent when the GPS provider (and other providers) is enabled / disabled.

    <action android:name="android.location.PROVIDERS_CHANGED" />
    
    0 讨论(0)
提交回复
热议问题