2012-04-10 1 views
1

私はcwac-Location Poller(here)を使用して、ユーザーの場所を常にポーリングし、場所に基づく通知を表示しています。これはすべて正常に動作していますが、別のBroadcastReceiverを添付しようとしています。そのため、アプリケーションがフォアグラウンドになっている場合、通知を現在のユーザーの場所にアニメーション表示する代わりに表示します。しかし何らかの理由で私はそれを働かせることはできません。私はポーラーを開始するには次のコードしているMapActivityLocationPollerと複数のブロードキャストレシーバー

onCreate()方法:onResume()方法で

@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);         
} 

を私はregisterReceiver()メソッドを使用して別の受信機を登録しています:

locationReceiverがどのように見える
@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); 
} 

private BroadcastReceiver locationReceiver = new BroadcastReceiver() {  
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d(TAG, "mapActivity called"); 
     abortBroadcast(); 
    } 
}; 
私は sendOrderedBroadcast代わりの sendBroadcast

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

     toBroadcast.putExtra(LocationPollerResult.LOCATION_KEY, location);   
     sendOrderedBroadcast(toBroadcast, null); 
     quit(); 
} 

を使用するようにLocationPollerServiceを変更した複数の受信機にブロードキャスト注文210

して送信するためには、今、問題が呼ばれることはありません飽きない私の動的に登録受信機であるが、AndroidManifest.xmlをで述べた一つが行います。

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

答えて

2

あなたの問題は、Javaで作成IntentFilterとあなた間の切断でありますあなたの質問には含まれていませんでした。 IntentFilterは、特定のアクション文字列を含むブロードキャストを期待しています。Intentには、createIntentToBroadcastOnCompletion()で作成しているように見えますが、このアクション文字列は含まれていません。

ところで、「複数の受信者にブロードキャストを送信するために」sendBroadcast()は、複数の受信者に完全にブロードキャストを送信することができます。

+0

以下は、私がcreateIntentToBroadcastOnCompletion(){return new intent(locationPollerParameter.getIntentToBroadcastOnCompletion())で持っているものです。 } getIntentToBroadcastOnCompletion()は、 "com.commonsware.cwac.locpoll.EXTRA_INTENT"と同じ文字列を返します。 – Waqas

+0

@Waqas:次に、 "com.commonsware.cwac.locpoll.EXTRA_INTENT"は 'com.commonswareと同じではありません.cwac.locpoll.LocationPollerParameter.INTENT_TO_BROADCAST_ON_COMPLETION_KEY'。 – CommonsWare

+0

あなたの素早い返信に感謝します.com.commonsware.cwac.locpoll.LocationPollerParameter.INTENT_TO_BROADCAST_ON_COM PLETION_KEYは値com.commonsware.cwac.locpoll.EXTRA_INTENTを持つ静的な文字列です。 – Waqas

関連する問題