Reputation: 882
Recently, I have developed an app on travelling domain. The aim of this app is to show the user; the path from source to destination. When the user comes in the range of 25 meters range of the destination, the user gets a notification/ an alert stating that the destination is nearby.
What I tried to achieve it: In the onLocationChanged() I kept on for the destination's range & if the user is in the range, the notification/alert will pop-out. However, when the I tested the app, I found out that when I am in the range, the notifications flood the device horribly as the condition of showing the notification/alert is based on the onLocationChanged() ie as location changes, the loop executes the exact same number of times and the user get annoyed by the app.
Also, the app does not work when I search for a different location. It does not show the destination marker. For the first time, the destination marker is seen but later searches do not show the destination marker; I wonder why?!
This problem has bugged me since long time. Please help me on this one!!
Upvotes: 1
Views: 703
Reputation: 67
When you are using loop in the onLocationChange() method, in side your loop take a variable say "count" and then check the condition for the distance. After the particular condition got true then increment the count. If counter get 1 then pop an alert and after that exit from the loop.
int count=0;
public void onLocationChanged(Location locFromGps) {
if(locFromGps<=30) {
if(count==1) {
exit(0);
}
count++;
Toast.makeText(getApplicationContext(),"Location is near", 0).show();
}
}
Upvotes: 1
Reputation: 3262
Two possible solutions that are probably best combined: one, use a static dialog so that you only have one instance of the alert; two, unregister the onLocationChanged() listener as soon as you reach the destination (the first time it alerts the user).
Upvotes: 1
Reputation: 644
you can keep a check on the number of time the notification is displayed, for example in loop
boolean notification_shown = false;
for(...)
{
if(!notification_shown)
{
//show notification
notification_shown = true;
}
else
{
//rest of your coding
}
}
this will show the notification only one time. If you want to display notification for few more times than you can use counter. like
int counter = 0;
//increment it till you want and than stop
Upvotes: 1