0

サーバーにロケーションの更新を送信するための更新間隔を使用して、ロケーションサービスを作成しています。しかし、ユーザー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

前もって感謝します。

4

1 に答える 1

1

updateIntervalの新しい値を取得するために作成したスレッドではなく、onCreate()でstartLocationListener()を呼び出しています。ただし、startLocationListener(updateInterval);の呼び出し。新しいスレッドが実行される前に実行されるため、updateIntervalの古い値を取得します。コードを次のように変更する必要があると思います。

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();
}
于 2012-04-16T13:22:34.303 に答える