2012-04-16 12 views
0

私は位置情報サービスを作成しており、位置更新をサーバーに送信するための更新間隔があります。 しかし、この入力変数をユーザー入力(AlertDialog)で更新しようとしました。ハードコーディングされたときは完璧に動作します。位置情報サービスの更新間隔Android

このコードを使用して、サービスのonCreate()AlertDialogクラスから間隔変数を取得しています。

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 

は、事前にありがとうございました。

+0

何が起こっているのですか?以前の更新レートでのみ動作していますか? – 5hssba

+0

@rajuこんにちは..はい、以前の更新レートでのみ機能します。 updateIntervalは、10000(ミリ秒)の値を持つグローバル変数です。 – crtn

答えて

1

onCreate()ではstartLocationListener()を呼び出していますが、updateIntervalの新しい値を取得するために作成したスレッドでは呼び出していません。しかし、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(); 
} 
+0

私はこれを試しました...しかし、私はこの例外を取得します。 – crtn

+0

04-16 08:15:43.850:E/AndroidRuntime(13728):java.lang.RuntimeException:Looper.prepare()を呼び出していないスレッド内でハンドラを作成できません。 – crtn

+0

startLocationListener()も起動していません。 – crtn

関連する問題