简体   繁体   中英

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)
            }
        };

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.
                }
            }
        };

You can used following code

if (getResultCode() == Activity.RESULT_OK ) {
 ...
}

by default you can not override onactivityResult method in Broadcast receiver but you can do it in following way :

  1. override onactivityResult method in any activty subclass
  2. storing result into sharedpreference
  3. access that value from onreceive method of broadcast using context

or initialize this type of global variable . and after compare with it.

private int resultCancel = Activity.RESULT_CANCELED;
private int resultOk = Activity.RESULT_OK;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM