This application is to get the number of available wifi by scanning and we know the broadcast receiver class actually used for getting alert. As example, when your battery is low or when you get an SMS alert and here we will use broadcast receiver to get the available number of WiFi and the number of connection change when we are going to access the WiFi.
Required software and tools for this application are:
- Eclipse IDE, you can use Eclipse Indigo or Juno or also Android Studio
- Android SDK
- At least one WiFi to connect
Please follow the steps below in order to make this application to connect a WiFi in Android device.
File – New- Android application project.
Give application name is for example 'WiFi connect'.
Project name and Package name
The next, select API level- android 4.0 ice cream sandwich. Target SDK API level 14 android 4.0 and compile with android 4.0 ice cream sandwich and theme to for ex. to holo light with dark action bar.
Then next change the text here for ex 'WiFi' and also change the foreground color.
Next activity is blank activity.
Then next activity name is MainActivity and the layout name is ActivityMain. And finish.
Now here is our activity main.xml.
So in the relative layout change all the padding.
Text view take android:id @+id/mainText.
Layout width and height are both wrap content.
Android text is @string/hello world. So change it to android:layout_centerHorizontal is true. Android:layout_centerVertical is true. Android:text @string/hello_world.
And at last, Save it.
Now I am gonna browse to main activity.java.
Now first, you have to declare text view 'tv'; WiFi manager 'wifi'; WiFi receiver;
And then declare WiFi receiver class. Now you have to create a class WiFi receiver, enclosing type 'wificonnect.MainActivity'.
Then declare the wifi list.
Now string builder sb = new String builder. Now inside on create method, write tv = (text view) findViewById (R.id.mainText). wifi = (WiFiManager)getSystemService (Context.WIFI_SERVICE)
Now if wifi is enabled – if (wifi.isWiFiEnabled() == false). Toast.make text.get application context. Text is wifi is disabled .. enabled it, Toast .wifi . set wifi enabled is true. Receiver = new wifi receiver. Then register receiver reciver new intent filter is wifi manager. Scan_Results_Available_Action.
Now import this package android.content. intentfilter.
Now wifi.start scan. Main text.set text starting scan. Tv.set text (starting scan).
Now we have to override the methods. Source override implement methods onResume and onPause.
Add inside on pause inside on resume method. Register receiver (receiver, new intent filter(wifi manager.SCAN_RESULTS_AVAILABLE_ACTIONS)). Super.onResume and inside on pause method write unregister receiver is receiver.
Now public class wifi receiver extends broadcast receiver.
Now add unimplemented methods. Public void on receiver context context, intent intent. Change context and intent intent. Then inside the on receive method write string builder sb = new String builder. Wifi list that is paste here wifilist = new Wifi list. Wifilist = wifi.getscan result. Sb.append ( wifi connection : +wifi list .size () + n). for (int I = 0; i<wifi list.size(); i++).
Now string builder sb.append (new Integer(I+1). To String()+). Sb.append (wifilist.get(i).to string). And then sb.append (). now write text tv.set text is sb.
Now wifi manager is wifi manager. Now save it.
Now test the application in your device in manifest write uses permission android name = "android.permission.ACCESS_WIFI_STATE" and
uses permission android name = "android permission.CHANGE_WIFI_STATE".