如何在回到同一活动后停止跟踪

时间:2015-09-28 12:00:07

标签: android timer latitude-longitude

我有一个显示经度和纬度的跟踪应用程序。

我有一个屏幕,有3个按钮showlocation,开始跟踪和停止跟踪。现在我想要的是跟踪将继续,直到我点击停止按钮。现在的情况是,当我保持相同的活动时,开始跟踪和停止跟踪工作正常但是当我只点击开始跟踪按钮并返回到其他活动然后返回到该特定活动并单击停止按钮活动崩溃,如果我点击开始跟踪按钮,跟踪再次开始,这意味着我执行的计时器正在工作2次。

所以我想要2个解决方案: 1.当一个计时器正在工作时,单击按钮到其他计时器将不会再次单击开始按钮。 2.当我回到活动并点击停止按钮时,计时器应该停止。

这是我的代码

public class GPSTrackingActivity extends Activity {

Timer timer;
TimerTask timerTask;   
final Handler handler = new Handler();

// GPSTracker class
GPStracker gps;
double longitude, latitude;
Button showLocation1,timerstopped,starttimer;

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_gpstracking);
 showLocation1 = (Button) findViewById(R.id.showlocation1);

 // show location button click event
 showLocation1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {        
            // create class object
            // create class object
            gps = new GPStracker(GPSTrackingActivity.this);

            // check if GPS enabled     
            if(gps.canGetLocation()){

                double latitude = gps.getLatitude();
                double longitude = gps.getLongitude();

                // \n is for new line
                Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();    
            }else{
                // can't get location
                // GPS or Network is not enabled
                // Ask user to enable GPS/network in settings
                gps.showSettingsAlert();
            }
        }
    });

 starttimer =(Button) findViewById(R.id.starttimer);

 // stop showing location button click event
 starttimer.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {                

            // create class object
            startTimer();
             Toast.makeText(getApplicationContext(), "Your tracking is now started " , Toast.LENGTH_SHORT).show();          

            }
    });



 timerstopped =(Button) findViewById(R.id.timerstopped);

 // stop showing location button click event
 timerstopped.setOnClickListener(new View.OnClickListener() {



        @Override
        public void onClick(View arg0) {
            timer.cancel();
            timerTask.cancel(); 
             Toast.makeText(getApplicationContext(), "Your tracking is now stopped " , Toast.LENGTH_SHORT).show();          

            }
    });

}




public void startTimer() {

            //set a new Timer       
          timer = new Timer();

          //initialize the TimerTask's job      
          gpsmethod();      

            //schedule the timer, after the first 1000ms the TimerTask will run every min

            timer.schedule(timerTask, 1000, 10000); //

        }


public void gpsmethod() {
    timerTask=new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            handler.post(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    GPStracker gps = new GPStracker(GPSTrackingActivity.this);
                    //call your function for getting lat n lon
                     double latitude = gps.getLatitude();
                   double longitude = gps.getLongitude();

                   Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }; 
}
}

0 个答案:

没有答案