Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for resultCode in Android's BroadcastReceiver?

I want to make a check if resultCode is RESULT_OK in Android's BroadcastReceiver's onReceive method like we do in onActivityResult method of an Activity, but how will I do that is my question.

Receiver's code is:

new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                //This is what I like to check.
                //if(resultCode == RESULT_OK)
            }
        };
like image 536
Master Avatar asked Feb 22 '14 16:02

Master


People also ask

How do I check if BroadcastReceiver is registered?

registerReceiver(BroadcastReceiver,IntentFilter) */ public Intent register(Context context, IntentFilter filter) { try { // ceph3us note: // here I propose to create // a isRegistered(Contex) method // as you can register receiver on different context // so you need to match against the same one :) // example by ...

What is the role of the onReceive () method in the BroadcastReceiver?

Creating a BroadcastReceiver The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context.

What is the life cycle of BroadcastReceiver?

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent) . Once your code returns from this function, the system considers the object to be finished and no longer active.

What is the method name in BroadcastReceiver receive the message?

onReceive. This method is called when the BroadcastReceiver is receiving an Intent broadcast.


2 Answers

To make a check of resultCode in BroadcastReceiver's onReceive(...) method, we can use getResultCode() method of BroadcastReceiver.

This will give us current resultCode (which can be the standard results

  • RESULT_CANCELED,
  • RESULT_OK

or any custom values starting at RESULT_FIRST_USER).

For the above question, its implementation is given as:

new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                //This is what I like to check.
                if(getResultCode() == Activity.RESULT_OK)
                {
                     //Your code here.
                }
            }
        };
like image 153
Master Avatar answered Oct 11 '22 01:10

Master


You can used following code

if (getResultCode() == Activity.RESULT_OK ) {
 ...
}
like image 22
Koray Güclü Avatar answered Oct 11 '22 03:10

Koray Güclü