从intentservice切换位置更新

时间:2013-08-07 02:18:02

标签: android gps intentservice looper

如果用户想要运行GPS,我的代码就是检查数据库。我需要运行这几分钟,我可以使用Intentservice来运行该方法。我已经尝试过实现Looper,但最终代码再也没有运行检查。没有Looper我得到Looper错误,我无法在没有调用Looper.prepare()的线程中创建一个处理程序;

Timer timer = new Timer ();
TimerTask gpsTask = new TimerTask () {
    @Override
    public void run() {
        // TODO Auto-generated method stub
        gpsPlotter();
    }
};

// schedule the task to run starting now and then every hour...
timer.schedule(gpsTask, 0, 200);


private void gpsPlotter(){
        // TODO Auto-generated method stub
            try{
                BeaconHandler dbg = new BeaconHandler(Ping.this);
                List<Beacon> beacons = dbg.getAllBeacons();
                int activecheck = 0;
                for(Beacon bn : beacons){
                    activecheck = activecheck + bn.getStatus();
                }
                //db.close();
                if(activecheck > 0 && updateset == 0){
                    pinger.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, reciever);
                    updateset = 1;
                    Log.d("GPS RESET", "Listener Set");
                }
                else if(activecheck == 0 && updateset == 1){
                    pinger.removeUpdates(reciever);
                    updateset = 0;
                    Log.d("GPS RESET", "Listener Removed");
                }
                else{
                    Log.d("GPS RESET", "No Modifications");
                }
            }
            catch(Exception e){
                Log.e("GPS RESET ERROR", e.getMessage(), e);
            }
        }

1 个答案:

答案 0 :(得分:0)

我发现了我的问题。当我开始请求位置更新时,我需要像这样注册它。

pinger.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, reciever, getMainLooper());