web-dev-qa-db-ja.com

BroadcastReceiverが順序付けされていないブロードキャスト中に結果を返そうとしています-PACKAGE_ADDED in Android=

以下のコードでこの例外が発生します。このコードの何が問題になっているのか、私にはわかりません。この例外を取り除くために私を助けてください。

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)

以下は私のコードです:

    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

setResultCode()を削除します。 setResultCode() のドキュメントで説明されているように、これは順序付けされたブロードキャストでのみ使用できます。

56
CommonsWare

それを修正するきれいな方法は次のようにすることでしょう:

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

参考: https://groups.google.com/forum/#!topic/Android-gcm/0J_RBzTQFHs

3
Simon Ninon