android动态注册监听网络变化异常

在使用广播接收器监听网络变化的时候,在AndroidManifest.xml中加入<user-permission android:name="android.permission.ACCESS_NETWOEK_STATE"/>还是无法启动应用程序,显示如下异常

07-06 00:03:22.568  22393-22393/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.lvkui.helloworld3, PID: 22393
    java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 (has extras) } in com.lvkui.helloworld3.MainActivity$NetworkChangeReceiver@1e433f1a
            at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:871)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10060 nor current process has android.permission.ACCESS_NETWORK_STATE.
            at android.os.Parcel.readException(Parcel.java:1540)
            at android.os.Parcel.readException(Parcel.java:1493)
            at android.net.IConnectivityManager$Stub$Proxy.getActiveNetworkInfo(IConnectivityManager.java:960)
            at android.net.ConnectivityManager.getActiveNetworkInfo(ConnectivityManager.java:603)
            at com.lvkui.helloworld3.MainActivity$NetworkChangeReceiver.onReceive(MainActivity.java:62)
            at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:861)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

解决方法是:

在application标签中再加入

android:sharedUserId="android.uid.system"

就可以

 

posted on 2015-07-06 12:23  kuiblog.com  阅读(2906)  评论(0编辑  收藏  举报