Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

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

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 most important.

There is no message output.

package com.sharelbs.lbs.service;  
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class GPStStatusReceiver extends BroadcastReceiver {
  public static final String GPS_ENABLED_CHANGE_ACTION = "android.location.GPS_ENABLED_CHANGE";
  @Override
    public void onReceive(Context context, Intent intent) {
      Log.d("---------log--------","GPS Status onReceive");
      if(intent.getAction().equals(GPS_ENABLED_CHANGE_ACTION)){
        Log.d("---------log--------","GPS Status Changed");
        startMyProgram();
      }
    }
}

Here is my Manifest.xml:

<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<receiver android:name=".service.GPStStatusReceiver"> 
  <intent-filter> 
    <action android:name="android.location.GPS_ENABLED_CHANGE" /> 
  </intent-filter> 
</receiver>
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...