Java 定位轮询器和多广播接收器

Java 定位轮询器和多广播接收器,java,android,broadcastreceiver,commonsware-cwac,Java,Android,Broadcastreceiver,Commonsware Cwac,我正在使用cwac位置轮询器(from)不断轮询用户位置并显示基于位置的通知。这一切都很好,但现在我正在尝试附加另一个BroadcastReceiver,以便如果我的应用程序位于前台,而不是显示通知,则将谷歌地图动画化到当前用户位置。但由于某种原因,我不能让它工作 onCreate()映射活动的方法我有以下代码来启动轮询器: @Override public void onCreate(Bundle savedInstanceState) { ..... alarmManager

我正在使用cwac位置轮询器(from)不断轮询用户位置并显示基于位置的通知。这一切都很好,但现在我正在尝试附加另一个
BroadcastReceiver
,以便如果我的应用程序位于前台,而不是显示通知,则将谷歌地图动画化到当前用户位置。但由于某种原因,我不能让它工作

onCreate()
映射活动的方法我有以下代码来启动轮询器:

@Override
public void onCreate(Bundle savedInstanceState) {
   .....

    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    Intent i = new Intent(this, LocationPoller.class);

    Bundle bundle = new Bundle();
    LocationPollerParameter parameter = new LocationPollerParameter(bundle);
    parameter.setIntentToBroadcastOnCompletion(new Intent(this, LocationReceiver.class));
    parameter.setProviders(new String[] {LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER});
    parameter.setTimeout(60000);
    i.putExtras(bundle);

    pendingIntent = PendingIntent.getBroadcast(this, 0, i, 0);

    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
                SystemClock.elapsedRealtime(), PERIOD, pendingIntent);                                 
}
onResume()
方法中,我正在使用
registerReceiver()
方法注册另一个接收器:

@Override
protected void onResume() {
    super.onResume();
    IntentFilter intentFilter = new IntentFilter(com.commonsware.cwac.locpoll.LocationPollerParameter.INTENT_TO_BROADCAST_ON_COMPLETION_KEY);
    intentFilter.setPriority(1);
    registerReceiver(locationReceiver, intentFilter);
}
其中locationReceiver看起来像:

private BroadcastReceiver locationReceiver = new BroadcastReceiver() {      
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "mapActivity called");
        abortBroadcast();
    }
};
为了向多个接收器发送有序广播,我修改了
LocationPollerService
以使用
sendOrderedBroadcast
而不是
sendBroadcast

public void onLocationChanged(Location location) {
      handler.removeCallbacks(onTimeout);
      Intent toBroadcast = createIntentToBroadcastOnCompletion();

      toBroadcast.putExtra(LocationPollerResult.LOCATION_KEY, location);        
      sendOrderedBroadcast(toBroadcast, null);
      quit();
}
现在的问题是,我的动态注册接收器从未被调用,但AndroidManifest.xml中提到的接收器会:

    <receiver android:name=".receiver.LocationReceiver" />
    <receiver android:name="com.commonsware.cwac.locpoll.LocationPoller" />
    <service android:name="com.commonsware.cwac.locpoll.LocationPollerService" />


您的问题在于您在Java中创建的
IntentFilter
与您的
createIntentToBroadcastOnCompletion()
实现之间的断开连接,您的问题中没有包括该实现。您的
IntentFilter
需要一个带有特定操作字符串的广播—您在
createIntentToBroadcastOnCompletion()
中创建的
Intent
显然不包括此操作字符串


顺便说一句,关于“并且为了向多个接收器发送广播,
sendBroadcast()
完全能够向多个接收器发送广播。

以下是我在CreateIntentToBroadcastionCompletion()中的内容{返回新的意图(locationPollerParameter.GetIntentToBroadcastionCompletion());}其中getIntentToBroadcastOnCompletion()返回与“com.commonware.cwac.locpoll.EXTRA_INTENT”完全相同的字符串@Waqas:Then
“com.commonware.cwac.locpoll.EXTRA_INTENT”
com.commonware.cwac.locpoll.LocationPollerParameter.INTENT\u TO\u BROADCAST\u ON\u COMPLETION\u键不同
。感谢您的快速回复-com.commonware.cwac.locpoll.LocationPollerParameter.INTENT\u TO\u BROADCAST\u ON\u‌​PLETION_KEY是一个静态字符串,其值为com.commonware.cwac.locpoll.EXTRA_INTENT“@Waqas:下面是一个使用有序广播的示例项目:--您需要确定您对广播所做的操作与此示例中的不同之处。谢谢!您对IntentFilter的看法是正确的-操作值不相同