位置变化时无限广播

时间:2011-04-19 11:00:38

标签: android

我在活动中制作了一个功能

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>

现在我的问题是任何位置都更新它无限播放我的模拟器挂起。 有一段时间它给出了空位置

1 个答案:

答案 0 :(得分:1)

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

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