I use addProximityAlert:
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);
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):
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:
And then I have this in my own class constructor:
I hope this will be useful for someone.