I am writing a Location Service, with a update Interval to send the Location updates to the server.
But I trying to update this interval variable in the service via user input(AlertDialog
). It works perfectly fine when hard coded.
I am using this code to get the interval variable from the AlertDialog
class, in the onCreate()
of the service.
public void onCreate() {
super.onCreate();
final boolean tr=true;
new Thread(new Runnable() {
public void run() {
while (tr) {
//check your static variable here
updateInterval=ShowCurInterval.loadCurInterval(getApplicationContext());//ShowCur Interval is the Alert Dialog calss
Log.d(" INTERVAL ","Interval "+ updateInterval);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}}
).start();
startLocationListener(updateInterval);
}
And I can also see the new updateInterval value in Log ( which is added from the Alert Dialog). But requestLocationUpdates()
still uses the pre defined updateInterval value.
Here is the startLocationListener() method:
public void startLocationListener(int updateInterval) {
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locManager.removeUpdates(locListener);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, updateInterval, updateDistance, locListener);
Log.d(getClass().getSimpleName(), "Loclistener started, Updatetime: " + updateInterval);
Toast.makeText(getApplicationContext(), "UPDATE INTERVAL"+updateInterval,Toast.LENGTH_SHORT );
preInterval = updateInterval;
}
Does anyone have any suggestions how can I update this variable?
@binW Edited part with exception:
Handler mhandler = new Handler ();
mhandler.postDelayed( new Runnable(){
public void run(){
Looper.prepare();
updateInterval=SingletonManager.getInstance().loadCurInterval(getApplicationContext());
SingletonManager.getInstance().saveCurInterval(getApplicationContext(), updateInterval);
startLocationListener(updateInterval);
Log.d(" INTERVAL ","Interval "+ updateInterval);
//startChecking();
}
}, 2000);
Exception:
04-17 03:18:55.250: E/AndroidRuntime(2146): java.lang.RuntimeException: Only one Looper may be created per thread
Thank You in advance.
you are calling startLocationListener() in onCreate() and not in the thread that you created for getting the new value of updateInterval. But the call to startLocationListener(updateInterval); gets executed before the new thread executes and hence you get the old value of updateInterval. I believe you should change your code to following:
public Handler handler;
public void onCreate() {
super.onCreate();
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
startLocationListener(updateInterval);
}
};
final boolean tr=true;
new Thread(new Runnable() {
public void run() {
while (tr) {
//check your static variable here
updateInterval=ShowCurInterval.loadCurInterval(getApplicationContext());//ShowCur Interval is the Alert Dialog calss
handler.sendEmptyMessage(1);
Log.d(" INTERVAL ","Interval "+ updateInterval);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}}
).start();
}