请参见下面的code
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.
请帮忙为什么会这样
要能够访问你必须使用Android的无线网络:NAME =android.permission.ACCESS_WIFI_STATE因为你已经知道
To be able to access the wifi you need android:name="android.permission.ACCESS_WIFI_STATE" as you already know.
如果要启用或禁用WiFi连接,你也将需要两个权限:ACCESS_WIFI_STATE和CHANGE_WIFI_STATE
If you are enabling or disabling the wifi connection you are also going to need both permissions: ACCESS_WIFI_STATE and CHANGE_WIFI_STATE
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
这应该为你做它。