サーバーにロケーションの更新を送信するための更新間隔を使用して、ロケーションサービスを作成しています。しかし、ユーザーinput(AlertDialog
)を介してサービスでこの間隔変数を更新しようとしています。ハードコーディングすると完全に正常に機能します。
AlertDialog
このコードを使用して、サービスのクラスから間隔変数を取得してonCreate()
います。
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);
}
また、ログ(アラートダイアログから追加された)に新しいupdateInterval値を表示することもできます。ただしrequestLocationUpdates()
、事前定義されたupdateInterval値を引き続き使用します。
startLocationListener()メソッドは次のとおりです。
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;
}
この変数を更新するにはどうすればよいですか?
@binW例外を除いて編集された部分:
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);
例外:
04-17 03:18:55.250: E/AndroidRuntime(2146): java.lang.RuntimeException: Only one Looper may be created per thread
前もって感謝します。