2011-07-07 2 views
2

を動作しません。IntentReceiveは、私はaddProximityAlert使用

public class MyBroadcastReciver extends BroadcastReceiver { 
    MyActivity mainActivity; 
    public MyBroadcastReciver(MyActivity activity) 
    { 
     mainActivity = activity; 
    } 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     mainActivity.ShowToast("Recived : " + intent.getAction()); 
    } 
} 

そしてMyActivityクラスでreciverを登録します:

public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     myReciver = new MyBroadcastReciver(this); 
     IntentFilter intentFilter = new IntentFilter(); 
intentFilter.addAction(getString(R.string.intent_message_location_fetched)); 
     registerReceiver(myReciver, intentFilter); 
... 
(R.string.intent_message_location_fetched = com.myapp.client.MyActivity.LocationFetched)

は、その後、私はBroadcastReciverクラスを作成しようとしています

LAT LONGの場所にいるときは何も起こりません。 どうしたの? マニフェストにReceiverを登録しようとしましたが、それは役に立たなかった。

P.S.私がgetBroadcastの代わりにgetActivityを使用したとき、これは正常に機能し、アクティビティが死んでいた場合はそれを復元しました。

+0

あなたは[android-intent]や[android-broadcastreceiver]などのAndroid固有のタグを使用する必要があります – styler1972

+0

マニフェストファイルなどでAndroid固有のタグを使用する必要がありますか? – Ksice

答えて

1

よろしくお願いします。とにかくTnx。 今、私はインテントがどのように機能するかを理解しています。 主なものは、私が別のコンテキストを使用していたことです(私はそうだと思います)。

今私はPendingIntentで呼び出されたaddProximityAlert関数を持つ自分のクラスを持っています。

//Prepare proximity Intent 
proximityIntent = new Intent(activityContext.getString(R.string.intent_message_location_fetched)); 
proximityPendingIntent = PendingIntent.getBroadcast(activityContext, 0, proximityIntent, 0); 
locationManager.addProximityAlert(latitude, longitude, (float) radius, -1, proximityPendingIntent); 

お知らせ満了値= -1:代わりにこのの私はWrapperContext activityContextを(私は私のクラスを作成していたとき、私は私の活動の模範を渡す)合格します!それを捕まえる前に、あなたの意図はおそらく死んでしまうでしょう。

二つ目 - 私はマニフェストでアクション=「@文字列/ my_message」と登録の意図・フィルタだ:

<activity ...> 
      ... 
     <intent-filter> 
       <action android:name="@string/intent_message_location_fetched"/> 
     </intent-filter> 
</activity> 

そして、私は自分のクラスのコンストラクタでこれを持っている:

//Create and register receiver 
     BroadcastReceiver mReceiver = new BroadcastReceiver() { 

      @Override 
      public void onReceive(Context context, Intent intent) { 
       if(intent.getAction().equals(context.getString(R.string.intent_message_location_fetched)) 
         && intent.getExtras().getBoolean(LocationManager.KEY_PROXIMITY_ENTERING)) 
       { 
        Toast.makeText(activityContext, "LOCATION INTENT WORKS!", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }; 
     activityContext.registerReceiver(mReceiver, new IntentFilter(activityContext.getString(R.string.intent_message_location_fetched))); 

これは誰かにとって便利だと思います。

関連する問題