Wifi切换Android代码错误

时间:2016-02-13 05:12:16

标签: android android-wifi

当我按下wifi切换按钮并且应用程序停止工作时,我收到此错误。它提到用户无权访问wifi状态。事情是它已在Manifest文件中声明。

  

使用的权限:

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  

错误

致命的例外:主要                                                                               处理:com.example.something.callblocker,PID:935                                                                               java.lang.SecurityException:WifiService:用户10052和当前进程都没有android.permission.ACCESS_WIFI_STATE。                                                                                   在android.os.Parcel.readException(Parcel.java:1472)                                                                                   在android.os.Parcel.readException(Parcel.java:1426)                                                                                   在android.net.wifi.IWifiManager $ Stub $ Proxy.getWifiEnabledState(IWifiManager.java:808)                                                                                   在android.net.wifi.WifiManager.getWifiState(WifiManager.java:1029)                                                                                   在android.net.wifi.WifiManager.isWifiEnabled(WifiManager.java:1041)                                                                                   在com.example.stamatis.callblocker.powersavings $ 2.onClick(powersavings.java:55)                                                                                   在android.view.View.performClick(View.java:4438)                                                                                   在android.widget.CompoundButton.performClick(CompoundButton.java:100)                                                                                   在android.view.View $ PerformClick.run(View.java:18422)                                                                                   在android.os.Handler.handleCallback(Handler.java:733)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                   在android.os.Looper.loop(Looper.java:136)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5001)                                                                                   at java.lang.reflect.Method.invokeNative(Native Method)                                                                                   在java.lang.reflect.Method.invoke(Method.java:515)                                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)                                                                                   在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:0)

如果发生这种情况并且您说清单中已经存在android.permission.ACCESS_WIFI_STATE,那么用户只有一种可能已明确停止此权限。

检查此崩溃是否在Marshmallow即API 23中.Google已为用户提供了明确删除应用权限的权限。

您可以阅读:http://www.greenbot.com/article/2990078/android/how-to-toggle-app-permissions-in-android-marshmallow.html

唯一的解决方案是在切换之前在运行时检查权限。 这是你如何做到这一点:

private boolean checkWriteExternalPermission()
{

    String permission = "android.permission.ACCESS_WIFI_STATE";
    int res = getContext().checkCallingOrSelfPermission(permission);
    return (res == PackageManager.PERMISSION_GRANTED);            
}