谷歌位置api位置监听器

时间:2016-01-23 06:13:06

标签: android google-api locationlistener

我正在使用谷歌位置api为我的应用程序,其中有一个位置监听器 -

FileOutputStream ouput_file = new FileOutputStream(new File(filename));
output_file = object_input_stream.readObject();

为了使我的应用程序正常工作,即使应用程序最小化,位置监听器也应该工作,但是当我点击主页按钮时,位置监听器将无法工作。当应用程序最小化时,如何使监听器工作?是否可以在备用线程上运行侦听器?怎么样?

1 个答案:

答案 0 :(得分:0)

没有人似乎知道答案,所以我不得不放弃,因为它已经很长时间了,而且仍然没有答案。

public void startTimer() {
    //set a new Timer
    timer = new Timer();
    //initialize the TimerTask's job
    initializeTimerTask();
    //schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
    timer.schedule(timerTask, 2000, 4000); //
}
public void initializeTimerTask() {
    timerTask = new TimerTask() {
        public void run() {

            //use a handler to run a toast that shows the current timestamp
            handler.post(new Runnable() {
                public void run() {
                    Runnable r = new Runnable() {
                        @Override
                        public void run() {
                            handler.post(new Runnable() {
                                public void run() {
                                    if (alarm){
                                        double distance;
                                        Location locationA = new Location("");
                                        locationA.setLatitude(destlat);
                                        locationA.setLongitude(destlng);
                                        Location locationB = new Location("");
                                        locationB.setLatitude(updLat);
                                        locationB.setLongitude(updLng);
                                        distance = locationA.distanceTo(locationB);
                                        current_distance.setText("Current distance: " + Math.round(distance) + " m");
                                        if(distance<rad){
                                            alertUser();
                                            alarm = false;
                                        }
                                    }
                                }
                            });
                        }
                    };
                    Thread bgAlarmThread = new Thread(r);
                    bgAlarmThread.start();
                }
            });
        }
    };

}