Broadcastreceiver - how to register for addProximityAlert method

Asked

Viewed 233 times

1

The snippet below has the mission to give a warning to the user if he entered an area of terrain that is a circle around the given geographical coordinates:

double mLatitude=35.41;
double mLongitude=139.46;

float mRadius=500f;

long expiration=-1;

Intent mIntent = new Intent ("Você entrou na área demarcada");

PendingIntent mFireIntent = PendingIntent.getBroadCast(this,-1,mIntent,0);

mLocationManager.addProximityAlert(mLatitude, mLongitude, mRadius, expiration, mFireIntent);

will not work by itself naturally because it is incomplete. Lack instantiation LocationManager and also register the BroadcastReceiver which, from what I understand, works almost like a Listener.

The IntentFilter associated with some action. And this is my problem.

How to register the Broadcastreceiver for the required action, which is triggered by the method addProximityAlert?

I did not find something similar to, for example,

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CAMERA_BUTTON);
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
registerReceiver(intentReceiver, intentFilter);

who would watch the click of a camera button.

(This object intentReceiver above - in the record of receiver - is the instantiation at another point of the code of a particular class that extended BroadcastReceiver, whose unique method onReceive would filter if there would be such a click on the camera button)

Ali, no IntentFilter parallel to my problem, the watched action is the click on the camera.

How to make a similar one in order to record the effective response of the method addProximityAlert?

1 answer

1


When creating Intentfilter you define the action you want. It is no more than a String.

You just have to make sure that this string is unique. Usually you use the package name plus something that identifies the action.

For example: namePackage.Proximityalert

The Intentfilter would then be instantiated:

IntentFilter intentFilter = new IntentFilter("nomePackage.ProximityAlert");  

Do not forget that the Intent should also be established with that IntentFilter:

Intent mIntent = new Intent ("nomePackage.ProximityAlert");

PendingIntent mFireIntent = PendingIntent.getBroadCast(this,-1,mIntent,0);

mLocationManager.addProximityAlert(mLatitude, mLongitude, mRadius, expiration, mFireIntent);  

To facilitate it is customary to declare a value static to save the filter string:

public static final String PROX_ALERT_INTENT = "nomePackage.ProximityAlert";
  • Dear @ramaral, I would like to call it off, but I think my 'reputation' still doesn’t allow it. Better, nor would know how to do it by the mechanisms of the forum. Thank you very much.

  • You only need to click on the "V" that is on the left side of the answer, next to the number zero. See here

  • I forgot to mention it. And that line that defines the action to be 'watched', which says intentFilter.addAction, for use in Broadcastreceiver onReceive is not required?

  • In this case no. In the constructor you are already indicating the Action. This is only necessary if you want the Intent respond to more than one action. Android documentation explains in detail how this works: link1, Link2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.