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)
}
};
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 ...
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.
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.
onReceive. This method is called when the BroadcastReceiver is receiving an Intent broadcast.
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
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 ) {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With