Using ResultReceiver in Android

Basically, I would like to set a callback for an Activity from an IntentService. My question is very similar to the one answered here:

Restful API Service

However, in the response code, the activity code is considered as an implementation of ResultReceiver. If I'm missing something, ResultReceiver is actually a class, so it cannot execute this implementation.

So, essentially, I am asking what would be the right way to connect ResultReceiver to this service. I am confused with the concepts of Handler and ResultReceiver regarding this. Any working code example would be appreciated.

+49
android callback android-activity service
Dec 22 '10 at 15:57
source share
5 answers
  • You need to create your own resultreceiver class, extended from ResultReceiver

  • then implements the resultreceiver interface in your activity

  • Pass the custom resultreceiver object for the intent of the Service and in the intenservice simply retrieves the recipient object and calls the receiver.send () function to send something that triggers the activity to the Bundle object.

    here's the custom class of ResultReceiver:

    public class MyResultReceiver extends ResultReceiver { private Receiver mReceiver; public MyResultReceiver(Handler handler) { super(handler); // TODO Auto-generated constructor stub } public interface Receiver { public void onReceiveResult(int resultCode, Bundle resultData); } public void setReceiver(Receiver receiver) { mReceiver = receiver; } @Override protected void onReceiveResult(int resultCode, Bundle resultData) { if (mReceiver != null) { mReceiver.onReceiveResult(resultCode, resultData); } } } 

implements the Myresultreceiver.receiver interface in your activity, creates a class variable

 Public MyResultReceiver mReceiver; 

initialize this variable in onCreate:

 mReceiver = new MyResultReceiver(new Handler()); mReceiver.setReceiver(this); 

Pass this mReceiver to the intent service through:

 intent.putExtra("receiverTag", mReceiver); 

and select in the IntentService, for example:

 ResultReceiver rec = intent.getParcelableExtra("receiverTag"); 

and send something into action using rec as:

 Bundle b=new Bundle(); rec.send(0, b); 

this will be received in onReceiveResult activity. You can view the full code at: IntentService: return data to activity

Edit: you must call setReceiver (this) in onResume and setReceiver (null) in onPause () to avoid leaks.

+96
Apr 22 '12 at 10:00
source share

You override the method by subclassing. This should not be the interface for this.

For example:

 intent.putExtra(StockService.REQUEST_RECEIVER_EXTRA, new ResultReceiver(null) { @Override protected void onReceiveResult(int resultCode, Bundle resultData) { if (resultCode == StockService.RESULT_ID_QUOTE) { ... } } }); 
+22
Dec 28 '10 at 21:23
source share

I created a simple example demonstrating how to use ResultReceiver .

Mainactivity

 public class MainActivity extends AppCompatActivity { private final static String TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent serviceIntent = new Intent(this, MyService.class); serviceIntent.putExtra("logName", "MAIN_ACTIVITY"); serviceIntent.putExtra(MyService.BUNDLED_LISTENER, new ResultReceiver(new Handler()) { @Override protected void onReceiveResult(int resultCode, Bundle resultData) { super.onReceiveResult(resultCode, resultData); if (resultCode == Activity.RESULT_OK) { String val = resultData.getString("value"); Log.i(TAG, "++++++++++++RESULT_OK+++++++++++ [" + val + "]"); } else { Log.i(TAG, "+++++++++++++RESULT_NOT_OK++++++++++++"); } } }); startService(serviceIntent); } } 

MyService

 public class MyService extends Service { private final static String TAG = MyService.class.getSimpleName(); public final static String BUNDLED_LISTENER = "listener"; @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { String logName = intent.getStringExtra("logName"); ResultReceiver receiver = intent.getParcelableExtra(MyService.BUNDLED_LISTENER); Bundle bundle = new Bundle(); bundle.putString("value", "30"); receiver.send(Activity.RESULT_OK, bundle); return Service.START_NOT_STICKY; } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } 
+2
Feb 04 '17 at 19:54
source share
0
Sep 06 '17 at 13:57 on
source share

for using Resulteceiver in android

  • Create SomeResultReceiver continues from resultReceiver

  • Create someReceiver interface using the on method, for example, onReceivResult (int resultCode, Bundle resultData);

3.use someReceiver in someResultreceiver

  1. create someService extends IntentService and use the someresultReceiver.send () method to send the result from the service to some class (for example: MyActivity)

  2. Implement somereceiver on Activity

6.instantiation someResultReceiver in class MyActivity and setreceiver

  1. startService with Intent and putExtra someResultreceiver instanse

For more information about the ResultReceiver class, see the link here.

-four
Jun 28 '15 at 20:14
source share



All Articles