android 8.0 无法接受到静态广播

解决办法:需要setComponent

Intent intent = new Intent();
intent.setAction("update_location");
intent.setComponent(new ComponentName(getApplicationContext().getPackageName(),
        "com.jiai.meisong.service.UpdateReceiver"));
sendBroadcast(intent);

ComponentName的参数1:app的包名;参数2:自定义receiver的路径。

出现的问题:

10-15 15:00:55.335 13460-13460/com.jiai.meisong E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.jiai.meisong, PID: 13460
    java.lang.RuntimeException: Unable to instantiate receiver com.jiai.meisong.service.UpdateReceiver: java.lang.IllegalAccessException: java.lang.Class is not accessible from java.lang.Class
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3369)
        at android.app.ActivityThread.-wrap17(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1827)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:192)
        at android.app.ActivityThread.main(ActivityThread.java:6779)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:445)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875)
     Caused by: java.lang.IllegalAccessException: java.lang.Class is not accessible from java.lang.Class
        at java.lang.Class.newInstance(Native Method)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3364)
        at android.app.ActivityThread.-wrap17(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1827)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:192)
        at android.app.ActivityThread.main(ActivityThread.java:6779)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:445)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875)

原因是:UpdateReceiver未用public

 

你可能感兴趣的:(广播)