android中访问wifi的安全异常
请参阅以下代码
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled() == false)
{
Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
wifi.setWifiEnabled(true);
}
虽然我已在清单文件中添加了权限,
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.wifi"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<user-permission android:name="android.permission.ACCESS_WIFI_STATE" />
但仍然给出以下错误
11-23 15:18:24.399: E/AndroidRuntime(6800): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.wifi/com.test.wifi.WifiDemoActivity}: java.lang.SecurityException: WifiService: Neither user 10082 nor current process has android.permission.ACCESS_WIFI_STATE.
请帮助解释为什么会发生这种情况
Please see the following code
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled() == false)
{
Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
wifi.setWifiEnabled(true);
}
Though I have added the permission in manifest file as
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.wifi"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<user-permission android:name="android.permission.ACCESS_WIFI_STATE" />
but still it is giving following error
11-23 15:18:24.399: E/AndroidRuntime(6800): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.wifi/com.test.wifi.WifiDemoActivity}: java.lang.SecurityException: WifiService: Neither user 10082 nor current process has android.permission.ACCESS_WIFI_STATE.
Please help why this is happening
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如您所知,为了能够访问 wifi,您需要 android:name="android.permission.ACCESS_WIFI_STATE" 。
如果您要启用或禁用 wifi 连接,您还需要两个权限:ACCESS_WIFI_STATE 和 CHANGE_WIFI_STATE
这应该可以为您完成。
To be able to access the wifi you need android:name="android.permission.ACCESS_WIFI_STATE" as you already know.
If you are enabling or disabling the wifi connection you are also going to need both permissions: ACCESS_WIFI_STATE and CHANGE_WIFI_STATE
That should do it for you.
添加以下几行
希望有帮助!
add following lines
hope they help!
它应该是“使用权限”而不是“用户权限”。拼写错误
It should be "uses-permission" rather than "user-permission". spelling is wrong