web-dev-qa-db-fra.com

BroadcastReceiver essayant de renvoyer le résultat lors d'une diffusion non ordonnée - PACKAGE_ADDED dans Android

J'obtiens cette exception dans mon code ci-dessous. je n'ai aucune idée de ce qui ne va pas avec ce code. Veuillez m'aider à me débarrasser de cette 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)

Ci-dessous mon 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**
}
25
rupesh

Supprimez setResultCode(). C'est uniquement pour une utilisation avec une diffusion ordonnée, comme décrit dans la documentation de setResultCode() .

56
CommonsWare

La manière la plus simple de résoudre ce problème serait de procéder comme suit:

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

Pour référence: https://groups.google.com/forum/#!topic/Android-gcm/0J_RBzTQFHs

3
Simon Ninon