1

デバイスのシャットダウン時の onHandleIntent の動作はどうなりますか?

IntentService では、onHandleIntent がジョブを完了しない限り、サービスが実行され続けることを知っています。

考えてみると、デバイスのシャットダウン時のサービスの動作に関する一般的な質問ですが、デバイスを再起動するとサービスは「目覚める」のでしょうか?

そうでない場合、そうする方法はありますか?何が起こっても、onHandleIntent が終了するまでインテントサービスを実行し続けたいと思います。

編集:理解を深めるためにコードを追加します。SQLite でリクエストを保存し、そのテーブルが空になるまで実行し続けようとしています。その後、サービスはシャットダウンします。そのため、デバイスがシャットダウンした場合でも、シャットダウン前と同じ場所から続行します。PS - 私はより良いパフォーマンスのためにエグゼキューターを使用しようとしています (これはテストであり、まだ証明されていません)。

onHandleIntent

@Override
protected void onHandleIntent(Intent intent) {
    helper = new DBHelper(getApplicationContext());
    executor = Executors.newFixedThreadPool(5);
    File file;
    Log.e("requestsExists",helper.requestsExists()+"");
    while (helper.requestsExists()) {
        ArrayList<String> requestArr = helper.getRequestsToExcute(5);
        //checks if the DB requests exists
        if (!requestArr.isEmpty()) {
            //execute them and delete the DB entry
            for(int i = 0; i < requestArr.size(); i++) {
                file = new File(requestArr.get(i));

                Log.e("file",file.toString());
                Future<String> future = executor.submit(new MyThread(file,getApplicationContext()));

                Log.e("future object", future.toString());
                try {
                    long idToDelete = Long.parseLong(future.get());
                    Log.e("THREAD ANSWER", future.get() + "");
                    helper.deleteRequest(idToDelete);
                } catch (InterruptedException e) {
                    Log.e("future try", "");
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    executor.shutdown();
}

マイスレッド

public class MyThread implements Callable {

    private File _file;
    private Context context;
    private DBHelper helper;

    public MyThread(File file, Context context) {
        this._file = file;
        this.context = context;
    }

    @Override
    public String call() throws Exception {
        HttpClient client = Utility.getNewHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost post = new HttpPost("http://192.168.9.62/mobile_api/timeline/moment/upload");
        try {
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

            FileBody fileBody = new FileBody(_file);
            builder.addPart("content", fileBody);
            builder.addPart("type", new StringBody("file", ContentType.TEXT_PLAIN));
            builder.addPart("title", new StringBody("service test", ContentType.TEXT_PLAIN));
            builder.addPart("userType", new StringBody("user", ContentType.TEXT_PLAIN));
            builder.addPart("uid", new StringBody(MyInfiActivity.friends_uid, ContentType.TEXT_PLAIN));
            builder.addPart("momentId", new StringBody("1", ContentType.TEXT_PLAIN));
            builder.addPart("storyId", new StringBody("8", ContentType.TEXT_PLAIN));
            Utility.addCookiesToPost(post);

            post.setEntity(builder.build());
            client.execute(post, localContext);
        } catch (IOException e) {
            Log.e("Callable try", post.toString());
        }
        return "1";
    }
}
4

2 に答える 2

1

私はあなたの質問に答えるためにポイントごとに行きます

デバイスのシャットダウン時の onHandleIntent の動作はどうなりますか?

デバイスが完全にオフになるため、 IntentService を含むすべてのサービスが強制的に停止されます。
ACTION_SHUTDOWN をサブスクライブして、デバイスがいつオフになるかを知り、実際にオフになる前にいくつかのことを行うことができます。

public class ShutdownHandlerReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //Handle here, but don't make actions that takes too long
    }    
}

また、これをマニフェストに追加する必要があります

<receiver android:name=".ShutdownHandlerReceiver">
  <intent-filter>
    <action android:name="android.intent.action.ACTION_SHUTDOWN" />
  </intent-filter>
</receiver>

デバイスを再起動すると、彼らは「目覚め」ますか?

いいえ、それらは既に破棄されているため、そうはなりませんが、ACTION_BOOT_COMPLETEDにサブスクライブして、デバイスの準備が整ったことを知ることができます。ドキュメントから:

ブロードキャスト アクション: これは、システムの起動が完了した後に 1 回ブロードキャストされます。アラームのインストールなど、アプリケーション固有の初期化を実行するために使用できます。このブロードキャストを受信するには、RECEIVE_BOOT_COMPLETED 権限を持っている必要があります。

   public class BootedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //Handle here, you can start again your stopped intentservices
    }    
}

そしてマニフェストで:

<receiver android:name=".BootedReceiver">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
  </intent-filter>
</receiver>

その後、インテンター サービスのキューを再起動できます。これが最善の方法だと思います。

于 2015-05-27T08:29:13.847 に答える