简体   繁体   中英

multiple proximity alert based on a service

I have my application, which uses ProximityAlerts to fire up when the user enters on the designated radios.

My proximityAlert fires up a service which shows a Toast telling me that I've entered the designated radio of the events.

The problem comes that I cannot make my application to fire several registered locations, it only react to the last one that was registered and ignore the earlier registered events

Any help please? I have seen people using broadcast receiver but in my case I use a service instead.

To build an Android app with proximity alert, the main ingredient are Broadcast Receiver, Intent, PendingIntent, and AddProximityAlert.

The workflow should be as following: -

  1. Register an IntentFilter with a Broadcast Receiver
  2. Create an Intent
  3. Get a PendingIntent that will perform a broadcast
  4. Set proximity alert

In order to make your app fire several registered locations, your must first register the broadcast receiver with correct IntentFilter and create the respective Intent for each Proximity Alert. Remember to use unique intent action for each location as follows

.....
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT + uniqueID);
.....
Intent mIntent = new Intent(PROX_ALERT_INTENT + uniqueID);
......

For more information, you can read my post on the topic

There are a couple points to be made here:

  1. Use a broadcast receiver to handle the PendingIntent that fires for a proximityAlert.
  2. When creating the PendingIntent for a proximityAlert, the PendingIntent must be unique for each location. The easiest way to do this is to create a unique action string for each location.

Here's an example of a PendingIntent you can create for a proximityAlert:

int uniqueID = <unique_id_for_location>;
String intentAction = "PROXIMITY_ALERT." + uniqueID;
PendingIntent proximityIntent = PendingIntent.getBroadcast(getApplicationContext(),
    uniqueID, new Intent(intentAction), PendingIntent.FLAG_CANCEL_CURRENT);

Then, add the proximityAlert for your location (I'm assuming you know how to do this part).

Next, register your broadcast receiver to handle the intent action string you created for your pendingIntent:

IntentFilter filter = new IntentFilter(intentAction);
registerReceiver( new ProximityItentReceiver(), filter );

Here, ProximityIntentReceiver is the name of the BroadcastReceiver class you have created.

Have you considered using a broadcast receiver then launching a service from the receiver?

When I first used them I found the system cached the results somewhat so had to set an ID (Which the docs claim not to be used). By doing this the intents weren't cached and I got the appropriate information to handle multiple proximity alerts.

Source code is available through github (See the bottom of this post) Gaunt Face - Multiple Proximity Alerts

It's all about the request_id in pendingIntent, you assign a requestid for pendingIntent..and if your first service uses the request id 1 means and second service uses the request id 2..so the first service not be killed and the service occurs concurrently ..

the count i used as request id in pending intent

 int COUNT=Integer.parseInt(some_text.getText().toString());
  if(COUNT==1)
 {
 PendingIntent  proxi_pi=PendingIntent.getService(class_name.this,COUNT,service_class_intent,Intent.FLAG_ACTIVITY_NEW_TASK);
   location_manager.addProximityAlert(location.getLatitude(), location.getLongitude(),   radius, -1, proxi_pi);
  }
  else if(COUNT==2)
  {
 PendingIntent   proxi_pi=PendingIntent.getService(class_name.this,count,service_class_intent,Intent.FLAG_ACTIVITY_NEW_TASK);
    location_manager.addProximityAlert(location.getLatitude(), location.getLongitude(),    radius, -1, proxi_pi);
  }
  else if(COUNT==so..on){
    }

i hope this helps you..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM