I am new to android. I am trying to add proximity alert for different locations that are retrieved from mysql database.I retrive locations and store them to an arrayList.
ArrayList<String> latArray, lngArray;
Now, I'm adding proximity alert to the locations like this:
for (int i = 0; i < latArray.size(); i++) {
setProximityAlert(Double.valueOf(latArray.get(i)),Double.valueOf(lngArray.get(i)));
}
private void setProximityAlert(double latitude, double longitude) {
Intent intent = new Intent(IntentToFire);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
locationManager.addProximityAlert(latitude, longitude, radius,expiration, proximityIntent);
// register the broadcast receiver to start listening for proximity alerts
filter = new IntentFilter(IntentToFire);
registerReceiver(myReceiver, filter);
}
Now my broadcast receiver class is as follows:
public class MyProximityAlertReceiver extends BroadcastReceiver{
private static final int NOTIFICATION_ID = 1000;
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING; //indicates whether proximity alert is entering or exiting
//Retrieve extended data from the intent and return false if no value of the desired type is stored with the
given name.
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
Log.d(getClass().getSimpleName(), "entering");
System.out.print("entering");
}
else {
Log.d(getClass().getSimpleName(), "exiting");
System.out.print("exiting");
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//Retrieve a PendingIntent that will start a new activity
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);
//create icons and other stuffs for notification
Notification notification = createNotification();
notification.setLatestEventInfo(context,
"Proximity Alert!", "In the area.", pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
}
........
}
As soon as I receive notification I get the following error: Error receiving broadcast Intent { act=com.app (has extras) }
On double clicking few lines of error code, it takes me to the line:
notificationManager.notify(NOTIFICATION_ID, notification);
I am also unregistering and re-registering receiver on pause and on restart:
@Override
protected void onPause() {
unregisterReceiver(myReceiver);
super.onPause();
}
@Override
protected void onRestart() {
registerReceiver(myReceiver, filter);
super.onRestart();
}
I don't understand what I amd doing wrong? Am I doing something wrong when adding proximity alert? Any help appreciated.