如果用户想让 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);
}
}