Android 位置变化时的无限广播

Android 位置变化时的无限广播,android,Android,我在活动中发挥了作用 if (locationManager != null && pendingIntent != null) { locationManager.removeUpdates(pendingIntent); } Intent intent = new Intent("com.app.android.tracker.LOCATION_READY"); pendingIntent = PendingIntent.getBroadcast(getApplicat

我在活动中发挥了作用

if (locationManager != null && pendingIntent != null) {
   locationManager.removeUpdates(pendingIntent);
}
Intent intent = new Intent("com.app.android.tracker.LOCATION_READY");
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
            0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

//Register for broadcast intents
locationManager.requestLocationUpdates(provider,120000,0,pendingIntent); }
在广播接收方面,我做了些什么

public class LocationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Logger.debug("LocationReceiver->onRceive");
        Bundle b = intent.getExtras();
        Location loc = (Location)b.get(android.location.LocationManager.KEY_LOCATION_CHANGED);
        Logger.debug("Loc:"+loc);
        if(loc != null){
            //
        }
    } }
<receiver android:name=".LocationReceiver">
            <intent-filter>
                <action android:name="com.app.android.tracker.LOCATION_READY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter> 
</receiver>
在清单上我已经做了

public class LocationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Logger.debug("LocationReceiver->onRceive");
        Bundle b = intent.getExtras();
        Location loc = (Location)b.get(android.location.LocationManager.KEY_LOCATION_CHANGED);
        Logger.debug("Loc:"+loc);
        if(loc != null){
            //
        }
    } }
<receiver android:name=".LocationReceiver">
            <intent-filter>
                <action android:name="com.app.android.tracker.LOCATION_READY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter> 
</receiver>

现在我的问题是,当任何位置被更新时,它会无限地广播,而我的模拟器是挂起的。
有时它会给出空位置

您需要停止请求位置更新。 您可以在onReceive方法上执行此操作,例如:

LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(this);