I have code which is checking the database if the user wants to have the GPS running. I need to run through this every couples of minutes can I am using a Intentservice to run run the method. I've tried implementing Looper but that ends up with the code never running the check again. Without the Looper I get the Looper error that I cannot create a handler in thread that has not called 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);
}
}
I found my problem. When I start request location updates I needed to register it like so.
pinger.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, reciever, getMainLooper());