IntentService responds to a dead ResultReceiver

The action creates an instance of ResultReceiver and overrides onReceiveResult. The activity then sends the Intent to the IntentService and includes ResultReceiver as an extra. After the IntentService has completed processing, it sends the message back to the ResultReceiver and processes it in onReceiveResult.

The problem is that the user goes from Activity, then the result is still sent back to ResultReceiver, which causes all types of problems. There is no way to stop this behavior? I tried to stop the IntentService in onDestroy () activity, but the result was sent back anyway.

Here is an example of Activity

public class Foo extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent i = new Intent(this, SampleIntentService.class); i.putExtra("receiver", mReceiver); startService(i); } @Override public void onDestroy() { stopService(new Intent(this, SampleIntentService.class)); mReceiver = null; super.onDestroy(); } ResultReceiver mReceiver = new ResultReceiver(new Handler()) { @Override protected void onReceiveResult(int resultCode, Bundle resultData) { // Handle response from IntentService here } }; } 

Here is an example IntentService

 public class SampleIntentService extends IntentService { public SampleIntentService() { super(SampleIntentService.class.getName()); } @Override protected void onHandleIntent(Intent intent) { ResultReceiver rec = intent.getParcelableExtra("receiver"); if (rec != null) { rec.send(200, null); } } } 
+6
source share
1 answer

I solved this problem by creating a custom ResultReceiver as follows.

 public class SampleResultReceiver extends ResultReceiver { private Receiver mReceiver; public SampleResultReceiver(Handler handler) { super(handler); } public void setReceiver(Receiver receiver) { mReceiver = receiver; } public interface Receiver { void onReceiveResult(int resultCode, Bundle resultData); } @Override protected void onReceiveResult(int resultCode, Bundle resultData) { if (mReceiver != null) { mReceiver.onReceiveResult(resultCode, resultData); } } } 

Then in my activity I do the following:

 public class Foo extends Activity implements Receiver { private SampleResultReceiver mReceiver; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mReceiver = new SampleResultReceiver(new Handler()); mReceiver.setReceiver(this); Intent i = new Intent(this, SampleIntentService.class); i.putExtra("receiver", mReceiver); startService(i); } @Override public void onDestroy() { if (mReceiver != null) { mReceiver.setReceiver(null); } super.onDestroy(); } @Override protected void onReceiveResult(int resultCode, Bundle resultData) { // Handle response from IntentService here } } 

This will lead to the fact that any messages sent to your custom ResultReceiver will not be completed anywhere after the destruction of Activity :)

+12
source

Source: https://habr.com/ru/post/954307/


All Articles