2011-07-07 15 views
2

我用addProximityAlert:IntentReceive不起作用

Intent intent = new Intent(getString(R.string.intent_message_location_fetched)); 
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0); 
locationManager.addProximityAlert(LAT, LONG, RADIUS, 0, proximityIntent); 

据我了解的意图与R.string.intent_message_location_fetched应​​的位置之后被解雇行动将围绕LAT长。 (R.string.intent_message_location_fetched = com.myapp.client.MyActivity.LocationFetched)

然后我试图创建BroadcastReciver类:

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); 
... 

但是,当我在LAT LONG位置时没有任何反应。 有什么问题? 我试过在清单中注册Receiver,但它没有帮助。

P.S.当我使用getActivity而不是getBroadcast时,这个工作正常,如果它已经死了,就恢复我的Activity。

+0

FYI您应该使用Android专用标签,如[android-intent]和[android-broadcastreceiver] – styler1972

+0

您的意思是我应该在清单文件或其他东西中使用Android专用标签? – Ksice

回答

1

好的,伙计们。无论如何,Tnx。 现在我想知道Intents的工作原理。 主要的是(我想是)我已经使用了不同的上下文。

现在我有我自己的类,用PendingIntent调用addProximityAlert函数。取而代之的我通过WrapperContext activityContext(当我创建我的课我通过我的活动的典范):

//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或者你的意图可能会在你抓住它之前就已经死了。

第二件事情 - 我登记的意图过滤用行动=“@字符串/ 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))); 

我希望这对别人有用。