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 はサブジェクトの Exception をスローします。この方法では、db4o にアクセスし、http クライアントを使用して、AlarmManager のアラームを更新します。ただし、UI は変更しません。OnBootReceiver でサービスを開始します。

どうすれば問題を解決できますか?

前もって感謝します。

編集後:

追加後Looper.prepare();、システムは最初の反復で OK を実行しますが、2 番目の反復ではインスタンス Looper.prepare();: java.lang.RuntimeException: Only one Looper may be created per thread で例外が発生しました

ありがとう!

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 に答える