0

ウェイクロックを正しく実装していなかったに違いありません。これが私がやった方法です。これは正しいです?

private static final String LOCK_NAME_STATIC="domination.shredAssistant.locationService";
private static volatile PowerManager.WakeLock lockStatic=null;

synchronized private static PowerManager.WakeLock getLock(Context context) {
    if (lockStatic==null) {
        PowerManager mgr=(PowerManager)context.getSystemService(Context.POWER_SERVICE);

        lockStatic=mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                                                    LOCK_NAME_STATIC);
        lockStatic.setReferenceCounted(true);
    }

    return(lockStatic);
}

およびonStartメソッド

//onStart
public void onStart( Intent intent, int startId ) {
      super.onStart( intent, startId );
      if (running == false) {
          Context ctxt=getApplicationContext();
          getLock(ctxt).acquire();
          running = true;
          readResorts();
          serviceHandler = new Handler();
          serviceHandler.postDelayed( new RunTask(),1000L );
          runtest=true;
          locupdate(0,0);
      }

}

getLock(ctxt).acquire();を使用しているのに、非アクティブな状態が続いた後もサービスが強制終了される理由がわかりません。ご協力いただきありがとうございます!-ドム

4

1 に答える 1

1

サービスがかなり長い間非アクティブであった場合、Androidはそれを強制終了します。通常、開発者はサービスを最優先に設定しますが、これによりバッテリーの寿命が短くなります。長時間実行されるサービスを維持する必要がある場合は、サービスクラスの新しいstartForeground APIを使用して、通知バーにアイコンを投稿する必要があります。

developer.android

必要に応じて、これをAndroid1.5および1.6と下位互換性のあるものにするクラスを作成しました。... ServiceForegrounder.java

于 2011-05-05T23:04:07.483 に答える