Beep inside location listener
<pre><code>package com.project.speedometer;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.project.simple_speedo.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


public class Activity2 extends Activity 
{
private static int mySpeedmph;
private LocationManager mlocManager;
private LocationListener mlocListener;
public static int unit_desired;
public static int beeped = 0;
public static float pastTime = 0;
public static RadioButton radio_km;
public static RadioButton radio_m;  

private AdView adView;
MediaPlayer player, player1;
Calendar c = Calendar.getInstance();    

protected void onPause()
{
    mlocManager.removeUpdates(mlocListener);
    super.onPause();
}
protected void onResume()
{
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
    super.onResume();
}

public void onCreate(Bundle savedInstanceState) 
{
    Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    if (display.getOrientation()==0)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        RelativeLayout Llayout = (RelativeLayout)findViewById(R.id.Llayout);   
        AdView adView   = new AdView(this,AdSize.BANNER, "xxxxx");        
        Llayout.addView(adView);   
        //adView.loadAd(new AdRequest()); 
    }
    else
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.landscape);
        RelativeLayout Llayout_ls = (RelativeLayout)findViewById(R.id.Llayout_ls);   
        adView  = new AdView(this, AdSize.BANNER, "xxxxxxxxxxxx");        
        Llayout_ls.addView(adView);  
        //adView.loadAd(new AdRequest()); 
    }           
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);    
}   

public class MyLocationListener implements LocationListener
{

    public void onLocationChanged(Location loc)
    {       
        float curTime = System.currentTimeMillis();


        loc.setSpeed((float)30);            
        if (loc.hasSpeed())
        {
            float mySpeed = loc.getSpeed();                                             
            float mySpeed_max = SpeedometerActivity.mymaxspeed; 
                           mySpeed_max = 45; 
            Typeface tf1 = Typeface.createFromAsset(getAssets(),"fonts/digital-7.ttf");                 
            TextView textSpeed = (TextView)findViewById(R.id.widget1);
            Activity2.mySpeedmph = (int) ((mySpeed*3600/(1600)));
            textSpeed.setText("SPEED MPH");  
            final TextView numberSpeed = (TextView)findViewById(R.id.widget2);
            numberSpeed.setText(""+Activity2.mySpeedmph+"");
            numberSpeed.setTypeface(tf1);       
            numberSpeed.setTextColor(Color.GREEN);                  

            if (Activity2.mySpeedmph &gt; mySpeed_max &amp;&amp; curTime &gt; pastTime+5000) 
            {
                numberSpeed.setTextColor(Color.RED);
                player = MediaPlayer.create(Activity2.this, R.raw.beep);  
                player.setLooping(false);     
                player.setVolume(100,100);         
                player.start();  
                pastTime = curTime;
            }
            else 
            {
                numberSpeed.setTextColor(Color.GREEN);
            }               

        }
    }
    public void onProviderDisabled(String provider)
    {
        Toast.makeText(getApplicationContext(), "GPS Disabled", Toast.LENGTH_SHORT).show();
    }
    public void onProviderEnabled(String provider)
    {
        Toast.makeText(getApplicationContext(), "GPS Enabled", Toast.LENGTH_SHORT).show();
    }
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    } 
}   
}
</code></pre>

<p>I am writing the code for an app that will "beep" when the current speed is more than the user set warning speed limit. The code shown above is written inside onLocationChanged(), but for some logical reason, it beeps only once and then stops, which tells me that it goes through the loop once and after which the pastTime and curTime loses track and the if condition is not logically true after and hence skipping the loop. The reason I wanted a delay of 5 seconds is to have enough time delay between the beeps and not have them overlap. I intialized pasTime with 0 at the very beginning of the activity. Any suggestion on a fix for this is appreciated. Also curTime = c.getTimeInMillis() each time there is a location change in the location listener. Activity2.mySpeedmph = 67 and  mySpeed_max = 45 are hardcoded for this debug scenario and so the if condition is true always.</p>
