protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WifiManager mainWifiObj;
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
class WifiScanReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
}
}
WifiScanReceiver wifiReciever = new WifiScanReceiver();
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
List<ScanResult> wifiScanList = mainWifiObj.getScanResults(); //error in this line
String data = wifiScanList.get(0).toString();
I am trying to scan the wifi and wants the specific access points signal strength. SO , I copied this code from the website : http://www.tutorialspoint.com/android/android_wi_fi.htm
When I try to run this code, my application crashes. I have not done anything except this. If I comment the last two lines, my application does not crashes and also even does not give me any output.
Can anybody please inform me, how can I stop this error. Error: App_name closed forcefully.
The error in the Log Cat is :
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wifisignalstrength/com.example.wifisignalstrength.MainActivity}: java.lang.SecurityException: WifiService: Neither user 10112 nor current process has android.permission.ACCESS_WIFI_STATE.
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
android.app.ActivityThread.access$600(ActivityThread.java:127)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(24463): at android.app.ActivityThread.main(ActivityThread.java:4511)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(24463): Caused by: java.lang.SecurityException: WifiService: Neither user 10112 nor current process has android.permission.ACCESS_WIFI_STATE.
E/AndroidRuntime(24463): at android.os.Parcel.readException(Parcel.java:1327)
android.os.Parcel.readException(Parcel.java:1281)
android.net.wifi.IWifiManager$Stub$Proxy.getScanResults(IWifiManager.java:688)
android.net.wifi.WifiManager.getScanResults(WifiManager.java:804)
com.example.wifisignalstrength.MainActivity.onCreate(MainActivity.java:47)
android.app.Activity.performCreate(Activity.java:4486)
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
... 11 more
add the permission
android.permission.ACCESS_WIFI_STATE
to your AndroidManifest.xml document