开发者

IntentReceiver doesn't work

I use addProximityAlert:

Intent intent = new Intent(getString(R.string.intent_message_location_fetched));
PendingIntent proximityIntent = PendingIntent.get开发者_StackOverflow中文版Broadcast(this, 0, intent, 0);
locationManager.addProximityAlert(LAT, LONG, RADIUS, 0, proximityIntent);

As I understand intent with action R.string.intent_message_location_fetched should be fired after location will be around LAT LONG. (R.string.intent_message_location_fetched = com.myapp.client.MyActivity.LocationFetched)

Then I'm trying to create BroadcastReceiver class:

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

And register receiver in MyActivity class:

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

But nothing happens when I'm in LAT LONG location. What's wrong? I've tried register Receiver in manifest, but it didn't help.

P.S. When I've used getActivity instead of getBroadcast this worked OK and restored my Activity if it was dead.


OK, guys. Tnx anyway. Now I understan how Intents works. The main thing was (I suppose so) that I've used different contexts.

Now I have my own class with addProximityAlert function called with PendingIntent. Instead of this I pass WrapperContext activityContext (when I'm creating my class I pass an exemplar of my Activity):

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

Notice the expiration value = -1 ! Or your intent probably will be dead before you catch it.

The second thing - I'm registered intent-filter with action="@strings/my_message" in manifest:

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

And then I have this in my own class constructor:

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

I hope this will be useful for someone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜