1

Доброго времени суток. Есть интент сервис запускается из активити. Как после обработки в сервисе дать знать вызвавшему активити о том что обработка закончена???

3
  • послать интент в ответ? а внутрь вложить дополнительные данные, что бы активити могла понять, что именно завершилось.
    – KoVadim
    24 мая 2014 в 11:50
  • а послать то как? startActivity(intent); Но тут вроде как перезапуск активности получится или я не прав?
    – alexmx
    24 мая 2014 в 11:54
  • 2
    [ResultReceiver][1] же, и не надо ничего изобретать. [1]: developer.android.com/reference/android/os/ResultReceiver.html
    – falstaf
    24 мая 2014 в 12:18

1 ответ 1

0
public static final int RESULT_FINISH = 1;

Intent intent = new Intent(getApplicationContext(), Service.class);
intent.putExtra("receiver", new ResultReceiver(new Handler()){
    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
         if(resultCode == RESULT_FINISH ){
             Toast.makeText(this, "Service finished", Toast.LENGTH_SHORT).show();
         }
    }
});
startService(intent);

В сервисе:

ResultReceiver receiver = intent.getParcelableExtra("receiver");

Bundle data = new Bundle();
receiver.send(RESULT_FINISH, data);

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.