4

ユーザーとアプリによって自動的にキューに入れられるintentserviceがあります。ユーザーがアプリケーションからログアウトしたときにキューに入れられたすべての保留中のインテントを強制終了できるようにする必要がありますが、それを機能させることができないようです。stopService()とstopself()を試しましたが、ユーザーがログアウトした後もインテントはintentserviceを起動し続けます。インテントのIDを取得しようとしますが、インテントサービスが開始されるたびに、インテントIDを保持する変数が空になるため、これは困難です。これが私のintentserviceコードです:

public class MainUploadIntentService extends IntentService {
private final String TAG = "MAINUPLOADINTSER";
private GMLHandsetApplication app = null;
private SimpleDateFormat sdf = null;
public boolean recStops = true;

public MainUploadIntentService() {
    super("Main Upload Intent Service");

    GMLHandsetApplication.writeToLogs(TAG,
            "GMLMainUploadIntentService Constructor");

}

@Override
protected void onHandleIntent(Intent intent) {
GMLHandsetApplication.writeToLogs(TAG, "onHandleIntent Started");
if (app == null) {
    app = (GMLHandsetApplication) getApplication();
}
uploadData(app);
    GMLHandsetApplication.writeToLogs(TAG, "onHandleIntent Finished");
}

@Override
public void onDestroy() {
GMLHandsetApplication.writeToLogs(TAG, "onDestroy Started");
app = null;
    stopSelf();
    GMLHandsetApplication.writeToLogs(TAG, "onDestroy completed");
}

public void uploadData(GMLHandsetApplication appl) {
    //All of my code that needs to be ran
}
4

3 に答える 3

0

考えただけですが、 onhandleintent の内部で、アプリが実行されているかどうかを確認する引数を使用できますか?そうでない場合は、コードを実行しないでください。例。アプリの開始時に、静的変数を持つことができます

boolean appRunning;

次にインテントの onhandle で、アクティビティの onPause または onDestroy の後に appRunning を false に設定すると、onhandleintent コードをブール値でラップできます。

 protected void onHandleIntent(final Intent intent) {
     if(MainActivity.appRunning){
       ...
     }
}

ちょっとした考え

于 2016-06-05T02:11:47.350 に答える
0

ここで は、いつネイティブ (Android NDK) ハンドルを解放する必要がありますか? メソッドをHangAroundIntentService持つクラスですcancelQueue()。クラスにはメソッドもあります

public static Intent markedAsCancelIntent(Intent intent)

インテントをキャンセル インテントに変換し、

public static boolean isCancelIntent(Intent intent).

このクラスは、オープンソースの Google のコードに基づいています。

于 2012-09-25T13:30:54.933 に答える