1

我使用服务每 3 小时更新一次 db4o 文件:

    @Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    Log.d("Service", "Service started");
    pref = new MyPreferences(getApplicationContext());
    addNotificacion();//update notifications

    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            Looper.prepare();
                            Log.d(TAG, "updating...");
            updateInService();
            addNotificacion();
            Log.d(TAG, "End of update");
        }
    }, 40000, 60000);

}

我对 addNotificacion() 方法没有问题,但 updateInService 抛出了主题的异常。在这种方法中,我访问 db4o,使用 http 客户端,更新 AlarmManager 的警报......但我不修改 UI。我在 OnBootReceiver 中启动服务。

我该如何解决我的问题?

提前致谢。

编辑后:

添加后Looper.prepare();,系统在第一次迭代中完成,但在第二次,我在实例 Looper.prepare(); 中有一个异常;:java.lang.RuntimeException:每个线程只能创建一个 Looper

谢谢!

4

2 回答 2

4

您正在从工作线程更新 UI。您需要在主线程中调用。你可以使用runOnUiThread

activity.runOnUiThread(new Runnable() {
  public void run() {
    //Update UI code
        updateInService();
        addNotificacion();

  }
});
于 2012-10-15T06:47:46.597 回答
1

您可能希望在方法实现Looper.loop()结束时调用。run()

http://developer.android.com/reference/android/os/Looper.html

于 2012-10-15T07:20:47.263 回答