29

I am getting this exception in my below given code. i don't have any idea what is wrong with this code. Please help me out to get rid of this exception.

05-23 23:33:49.853: E/BroadcastReceiver(26895): BroadcastReceiver trying to return result during a non-ordered broadcast
05-23 23:33:49.853: E/BroadcastReceiver(26895): java.lang.RuntimeException: BroadcastReceiver trying to return result during a non-ordered broadcast
05-23 23:33:49.853: E/BroadcastReceiver(26895):     at android.content.BroadcastReceiver.checkSynchronousHint(BroadcastReceiver.java:783)
05-23 23:33:49.853: E/BroadcastReceiver(26895):     at android.content.BroadcastReceiver.setResultCode(BroadcastReceiver.java:549)
05-23 23:33:49.853: E/BroadcastReceiver(26895):     at com.waypedia.rupesh.abhi.RetentionApplicationAddedBroadcastReceiver.onReceive(RetentionApplicationAddedBroadcastReceiver.java:18)
05-23 23:33:49.853: E/BroadcastReceiver(26895):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2446)
05-23 23:33:49.853: E/BroadcastReceiver(26895):     at android.app.ActivityThread.access$1700(ActivityThread.java:139)
05-23 23:33:49.853: E/BroadcastReceiver(26895):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
05-23 23:33:49.853: E/BroadcastReceiver(26895):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-23 23:33:49.853: E/BroadcastReceiver(26895):     at android.os.Looper.loop(Looper.java:136)
05-23 23:33:49.853: E/BroadcastReceiver(26895):     at android.app.ActivityThread.main(ActivityThread.java:5102)
05-23 23:33:49.853: E/BroadcastReceiver(26895):     at java.lang.reflect.Method.invokeNative(Native Method)
05-23 23:33:49.853: E/BroadcastReceiver(26895):     at java.lang.reflect.Method.invoke(Method.java:515)
05-23 23:33:49.853: E/BroadcastReceiver(26895):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-23 23:33:49.853: E/BroadcastReceiver(26895):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-23 23:33:49.853: E/BroadcastReceiver(26895):     at dalvik.system.NativeStart.main(Native Method)

Below is my code:

    public class RetentionApplicationAddedBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
public void onReceive(Context context, Intent intent) {

    ComponentName comp = new ComponentName(context.getPackageName(),
            RetentionAddIntentService.class.getName());
    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK); //**LINE 18**
}
rupesh
  • 2,865
  • 4
  • 24
  • 50

2 Answers2

58

Delete setResultCode(). That is only for use with an ordered broadcast, as is described in the documentation for setResultCode().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 8
    This may not solve the issue for Android O devices. I got rid of `setResultCode()` in my class, and still getting this error on Android O from WakefulBroadcastReceiver. – IgorGanapolsky Aug 08 '17 at 20:36
  • @IgorGanapolsky: That doesn't make a lot of sense, as that specific error should only come about by setting a result. You might consider posting a separate Stack Overflow question with a [mcve] (code + stack trace). – CommonsWare Aug 08 '17 at 20:37
4

The clean way to fix it would be to do as follows:

if (isOrderedBroadcast()) {
    setResultCode(Activity.RESULT_OK);
}

For reference: https://groups.google.com/forum/#!topic/android-gcm/0J_RBzTQFHs

Simon Ninon
  • 2,371
  • 26
  • 43