8

据我所知,在清单中,我已经正确地声明了接收器元素。但是当我发出广播时,接收器永远不会被调用。日志猫显示。

07-22 23:51:49.181: E/AndroidRuntime(3799): FATAL EXCEPTION: main
07-22 23:51:49.181: E/AndroidRuntime(3799): java.lang.RuntimeException: Unable to instantiate receiver com.example.orderedbroadcastreceiver.HigherPriorityReceiver: java.lang.IllegalAccessException: access to class not allowed
07-22 23:51:49.181: E/AndroidRuntime(3799):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2333)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at android.app.ActivityThread.access$1500(ActivityThread.java:139)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at android.os.Looper.loop(Looper.java:137)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at android.app.ActivityThread.main(ActivityThread.java:4899)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at java.lang.reflect.Method.invokeNative(Native Method)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at java.lang.reflect.Method.invoke(Method.java:511)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at dalvik.system.NativeStart.main(Native Method)
07-22 23:51:49.181: E/AndroidRuntime(3799): Caused by: java.lang.IllegalAccessException: access to class not allowed
07-22 23:51:49.181: E/AndroidRuntime(3799):     at java.lang.Class.newInstanceImpl(Native Method)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at java.lang.Class.newInstance(Class.java:1319)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2328)
07-22 23:51:49.181: E/AndroidRuntime(3799):     ... 10 more

代码

class HigherPriorityReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "HigherPriorityReceiver", 0).show();
    setResultCode(1985);
    setResultData("DOB");
    //abortBroadcast();
}
4

3 回答 3

36

让课堂HigherPriorityReceiver成为public课堂

于 2013-07-22T19:30:34.963 回答
6

在 Kotlin 中,它必须是一个类,而不是一个对象。

于 2018-04-09T11:34:01.787 回答
4

回答

这个类必须是公共类

代码

public class HigherPriorityReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "HigherPriorityReceiver", 0).show();
        setResultCode(1985);
        setResultData("DOB");
        //abortBroadcast();
    }
}
于 2017-08-01T12:46:53.677 回答