更新Android位置服务的时间间隔

时间:2012-04-16 09:48:50

标签: android service locationlistener

我正在编写位置服务,并使用更新间隔将位置更新发送到服务器。 但我尝试通过用户输入(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);
}

我还可以在Log中看到新的updateInterval值(从Alert Dialog添加)。但是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

提前谢谢你。

1 个答案:

答案 0 :(得分: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();
}