android M(6.0)中启用热点时出错

时间:2017-05-22 06:00:13

标签: android

在Android M(6.0)中启用热点时出错,这是我的代码

WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = "test_network";
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

Method setWifiApMethod = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
boolean apStatus = (Boolean) setWifiApMethod.invoke(mWifiManager, netConfig, true);

清单中的瑕疵

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

启用热点时出错

java.lang.reflect.InvocationTargetException
         at java.lang.reflect.Method.invoke(Native Method)
         at com.whitebyte.wifihotspotutils.WifiApManager.setWifiApEnabled(WifiApManager.java:57)
         at com.whitebyte.hotspotcontrolexample.Main.onMenuItemSelected(Main.java:81)
         at com.android.internal.policy.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1217)
         at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
         at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
         at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
         at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
         at com.android.internal.view.menu.MenuPopup.onItemClick(MenuPopup.java:128)
         at android.widget.AdapterView.performItemClick(AdapterView.java:310)
         at android.widget.AbsListView.performItemClick(AbsListView.java:1155)
         at android.widget.AbsListView$PerformClick.run(AbsListView.java:3152)
         at android.widget.AbsListView$3.run(AbsListView.java:4067)
         at android.os.Handler.handleCallback(Handler.java:751)
         at android.os.Handler.dispatchMessage(Handler.java:95)
         at android.os.Looper.loop(Looper.java:154)
         at android.app.ActivityThread.main(ActivityThread.java:6123)
         at java.lang.reflect.Method.invoke(Native Method)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
      Caused by: java.lang.SecurityException: com.whitebyte.hotspotclients was not granted  this permission: android.permission.WRITE_SETTINGS.
         at android.os.Parcel.readException(Parcel.java:1683)
         at android.os.Parcel.readException(Parcel.java:1636)
         at android.net.wifi.IWifiManager$Stub$Proxy.setWifiApEnabled(IWifiManager.java:1681)
         at android.net.wifi.WifiManager.setWifiApEnabled(WifiManager.java:1606)
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.whitebyte.wifihotspotutils.WifiApManager.setWifiApEnabled(WifiApManager.java:57) 
         at com.whitebyte.hotspotcontrolexample.Main.onMenuItemSelected(Main.java:81) 
         at com.android.internal.policy.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1217) 
         at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761) 
         at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152) 
         at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904) 
         at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894) 
         at com.android.internal.view.menu.MenuPopup.onItemClick(MenuPopup.java:128) 
         at android.widget.AdapterView.performItemClick(AdapterView.java:310) 
         at android.widget.AbsListView.performItemClick(AbsListView.java:1155) 
         at android.widget.AbsListView$PerformClick.run(AbsListView.java:3152) 
         at android.widget.AbsListView$3.run(AbsListView.java:4067) 
         at android.os.Handler.handleCallback(Handler.java:751) 
         at android.os.Handler.dispatchMessage(Handler.java:95) 
         at android.os.Looper.loop(Looper.java:154) 
         at android.app.ActivityThread.main(ActivityThread.java:6123) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 

2 个答案:

答案 0 :(得分:2)

您收到错误是因为您不允许权限,因为启动Android 6.0您必须明确要求权限。

首先在清单

中声明权限
boolean permission;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        permission = Settings.System.canWrite(context);
    } else {
        permission = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED;
    }
    if (permission) {
        //do your code
    }  else {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
            intent.setData(Uri.parse("package:" + context.getPackageName()));
            context.startActivityForResult(intent, MainActivity.CODE_WRITE_SETTINGS_PERMISSION);
        } else {
            ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.WRITE_SETTINGS}, MainActivity.CODE_WRITE_SETTINGS_PERMISSION);
        }
    }

然后请求许可。

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>

答案 1 :(得分:1)

根据错误,您需要包含权限

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

在清单中的应用程序代码之外。