Android SecurityException ACCESS_WIFI_STATE

YoYoMa picture YoYoMa · Feb 17, 2012 · Viewed 9.5k times · Source

I am building an Android app that uses wifi.
I have properly declared these uses permissions in the manifest, however for some reason the app is throwing a SecurityException that causes the app to force-close.

I traced the cause of the security exception in LogCat to the system saying it doesn't have the access_wifi_state permission. This is strange and confusing to me because I have already declared it in the manifest.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.wifi"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="ACCESS_WIFI_STATE" />
    <uses-permission android:name="CHANGE_WIFI_STATE" />
    <uses-feature android:name="android.hardware.wifi" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-sdk android:minSdkVersion="8" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <activity
            android:name="android.wifi.AndroidWiFiActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And here is the code that causes the exception:

package android.wifi;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.wifi.R;

public class AndroidWiFiActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       TextView tv = (TextView) findViewById(R.id.textbox);
       tv.setText("ABC 123");
       WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
       if(wifiMgr.isWifiEnabled())
       {
           tv.append("Wifi is enabled");
       }
       else
       {
           tv.append("Wifi is disabled");
       }

  }
}

Any help is greatly appreciated. Thanks.

Answer

MByD picture MByD · Feb 17, 2012

The permission is:

android.permission.ACCESS_WIFI_STATE