Android - Wi-Fi Tutorial

<p >Android allows applications to access to view the access the state of the wireless connections at very low level. Application can access almost all the information of a wifi connection.</p> <p >The information that an application can access includes connected network&#39;s link speed,IP address, negotiation state, other networks information. Applications can also scan, add, save, terminate and initiate Wi-Fi connections.</p> <p >Android provides<b >WifiManager</b>API to manage all aspects of WIFI connectivity. We can instantiate this class by calling<b >getSystemService</b>method. Its syntax is given below &minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="typ" >WifiManager</span><span class="pln" > mainWifiObj</span><span class="pun" >;</span><span class="pln" > mainWifiObj </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >WifiManager</span><span class="pun" >)</span><span class="pln" > getSystemService</span><span class="pun" >(</span><span class="typ" >Context</span><span class="pun" >.</span><span class="pln" >WIFI_SERVICE</span><span class="pun" >);</span><span class="pln" > </span></pre> <p >In order to scan a list of wireless networks, you also need to register your BroadcastReceiver. It can be registered using<b >registerReceiver</b>method with argument of your receiver class object. Its syntax is given below &minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="kwd" >class</span><span class="pln" > </span><span class="typ" >WifiScanReceiver</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >BroadcastReceiver</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onReceive</span><span class="pun" >(</span><span class="typ" >Context</span><span class="pln" > c</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >Intent</span><span class="pln" > intent</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="typ" >WifiScanReceiver</span><span class="pln" > wifiReciever </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >WifiScanReceiver</span><span class="pun" >();</span><span class="pln" > registerReceiver</span><span class="pun" >(</span><span class="pln" >wifiReciever</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >IntentFilter</span><span class="pun" >(</span><span class="typ" >WifiManager</span><span class="pun" >.</span><span class="pln" >SCAN_RESULTS_AVAILABLE_ACTION</span><span class="pun" >));</span><span class="pln" > </span></pre> <p >The wifi scan can be start by calling the<b >startScan</b>method of the WifiManager class. This method returns a list of ScanResult objects. You can access any object by calling the<b >get</b>method of list. Its syntax is given below &minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="typ" >List</span><span class="pun" >&lt;</span><span class="typ" >ScanResult</span><span class="pun" >&gt;</span><span class="pln" > wifiScanList </span><span class="pun" >=</span><span class="pln" > mainWifiObj</span><span class="pun" >.</span><span class="pln" >getScanResults</span><span class="pun" >();</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > data </span><span class="pun" >=</span><span class="pln" > wifiScanList</span><span class="pun" >.</span><span class="kwd" >get</span><span class="pun" >(</span><span class="lit" >0</span><span class="pun" >).</span><span class="pln" >toString</span><span class="pun" >();</span></pre> <p >Apart from just Scanning, you can have more control over your WIFI by using the methods defined in WifiManager class. They are listed as follows &minus;</p> <table class="table table-bordered" > <tbody > <tr > <th >Sr.No</th> <th >Method &amp; Description</th> </tr> <tr > <td >1</td> <td ><b >addNetwork(WifiConfiguration config)</b> <p >This method add a new network description to the set of configured networks.</p> </td> </tr> <tr > <td >2</td> <td ><b >createWifiLock(String tag)</b> <p >This method creates a new WifiLock.</p> </td> </tr> <tr > <td >3</td> <td ><b >disconnect()</b> <p >This method disassociate from the currently active access point.</p> </td> </tr> <tr > <td >4</td> <td ><b >enableNetwork(int netId, boolean disableOthers)</b> <p >This method allow a previously configured network to be associated with.</p> </td> </tr> <tr > <td >5</td> <td ><b >getWifiState()</b> <p >This method gets the Wi-Fi enabled state</p> </td> </tr> <tr > <td >6</td> <td ><b >isWifiEnabled()</b> <p >This method return whether Wi-Fi is enabled or disabled.</p> </td> </tr> <tr > <td >7</td> <td ><b >setWifiEnabled(boolean enabled)</b> <p >This method enable or disable Wi-Fi.</p> </td> </tr> <tr > <td >8</td> <td ><b >updateNetwork(WifiConfiguration config)</b> <p >This method update the network description of an existing configured network.</p> </td> </tr> </tbody> </table> <h2 >Example</h2> <p >Here is an example demonstrating the use of WIFI. It creates a basic application that scans a list of wireless networks and populate them in a list view.</p> <p >To experiment with this example, you need to run this on an actual device on which wifi is turned on.</p> <table class="table table-bordered" > <tbody > <tr > <th >Steps</th> <th >Description</th> </tr> <tr > <td >1</td> <td >You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.</td> </tr> <tr > <td >2</td> <td >Modify src/MainActivity.java file to add WebView code.</td> </tr> <tr > <td >3</td> <td >Modify the res/layout/activity_main to add respective XML components</td> </tr> <tr > <td >4</td> <td >Modify the AndroidManifest.xml to add the necessary permissions</td> </tr> <tr > <td >5</td> <td >Run the application and choose a running android device and install the application on it and verify the results.</td> </tr> </tbody> </table> <p >Following is the content of the modified main activity file<b >src/MainActivity.java</b>.</p> <pre class="prettyprint notranslate prettyprinted" > <span class="kwd" >package</span><span class="pln" > com</span><span class="pun" >.</span><span class="pln" >example</span><span class="pun" >.</span><span class="pln" >sairamkrishna</span><span class="pun" >.</span><span class="pln" >myapplication</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >app</span><span class="pun" >.</span><span class="typ" >Activity</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >BroadcastReceiver</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >Context</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >Intent</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >IntentFilter</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >graphics</span><span class="pun" >.</span><span class="typ" >Color</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >net</span><span class="pun" >.</span><span class="pln" >wifi</span><span class="pun" >.</span><span class="typ" >ScanResult</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >net</span><span class="pun" >.</span><span class="pln" >wifi</span><span class="pun" >.</span><span class="typ" >WifiManager</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >os</span><span class="pun" >.</span><span class="typ" >Bundle</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >Menu</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >MenuItem</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >View</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >ArrayAdapter</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >Button</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >EditText</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >ListView</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >TextView</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >Toast</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > java</span><span class="pun" >.</span><span class="pln" >util</span><span class="pun" >.</span><span class="typ" >List</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >MainActivity</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >Activity</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >ListView</span><span class="pln" > lv</span><span class="pun" >;</span><span class="pln" > </span><span class="typ" >WifiManager</span><span class="pln" > wifi</span><span class="pun" >;</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > wifis</span><span class="pun" >[];</span><span class="pln" > </span><span class="typ" >WifiScanReceiver</span><span class="pln" > wifiReciever</span><span class="pun" >;</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >protected</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onCreate</span><span class="pun" >(</span><span class="typ" >Bundle</span><span class="pln" > savedInstanceState</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onCreate</span><span class="pun" >(</span><span class="pln" >savedInstanceState</span><span class="pun" >);</span><span class="pln" > setContentView</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >layout</span><span class="pun" >.</span><span class="pln" >activity_main</span><span class="pun" >);</span><span class="pln" > lv</span><span class="pun" >=(</span><span class="typ" >ListView</span><span class="pun" >)</span><span class="pln" >findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >listView</span><span class="pun" >);</span><span class="pln" > wifi</span><span class="pun" >=(</span><span class="typ" >WifiManager</span><span class="pun" >)</span><span class="pln" >getSystemService</span><span class="pun" >(</span><span class="typ" >Context</span><span class="pun" >.</span><span class="pln" >WIFI_SERVICE</span><span class="pun" >);</span><span class="pln" > wifiReciever </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >WifiScanReceiver</span><span class="pun" >();</span><span class="pln" > wifi</span><span class="pun" >.</span><span class="pln" >startScan</span><span class="pun" >();</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="kwd" >protected</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onPause</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > unregisterReceiver</span><span class="pun" >(</span><span class="pln" >wifiReciever</span><span class="pun" >);</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onPause</span><span class="pun" >();</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="kwd" >protected</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onResume</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > registerReceiver</span><span class="pun" >(</span><span class="pln" >wifiReciever</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >IntentFilter</span><span class="pun" >(</span><span class="typ" >WifiManager</span><span class="pun" >.</span><span class="pln" >SCAN_RESULTS_AVAILABLE_ACTION</span><span class="pun" >));</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onResume</span><span class="pun" >();</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onCreateOptionsMenu</span><span class="pun" >(</span><span class="typ" >Menu</span><span class="pln" > menu</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="com" >// Inflate the menu; this adds items to the action bar if it is present.</span><span class="pln" > getMenuInflater</span><span class="pun" >().</span><span class="pln" >inflate</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >menu</span><span class="pun" >.</span><span class="pln" >menu_main</span><span class="pun" >,</span><span class="pln" > menu</span><span class="pun" >);</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onOptionsItemSelected</span><span class="pun" >(</span><span class="typ" >MenuItem</span><span class="pln" > item</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="com" >// Handle action bar item clicks here. The action bar will</span><span class="pln" > </span><span class="com" >// automatically handle clicks on the Home/Up button, so long</span><span class="pln" > </span><span class="com" >// as you specify a parent activity in AndroidManifest.xml.</span><span class="pln" > </span><span class="kwd" >int</span><span class="pln" > id </span><span class="pun" >=</span><span class="pln" > item</span><span class="pun" >.</span><span class="pln" >getItemId</span><span class="pun" >();</span><span class="pln" > </span><span class="com" >//noinspection SimplifiableIfStatement</span><span class="pln" > </span><span class="kwd" >if</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >id </span><span class="pun" >==</span><span class="pln" > R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >action_settings</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onOptionsItemSelected</span><span class="pun" >(</span><span class="pln" >item</span><span class="pun" >);</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="kwd" >private</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >WifiScanReceiver</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >BroadcastReceiver</span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onReceive</span><span class="pun" >(</span><span class="typ" >Context</span><span class="pln" > c</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >Intent</span><span class="pln" > intent</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >List</span><span class="pun" >&lt;</span><span class="typ" >ScanResult</span><span class="pun" >&gt;</span><span class="pln" > wifiScanList </span><span class="pun" >=</span><span class="pln" > wifi</span><span class="pun" >.</span><span class="pln" >getScanResults</span><span class="pun" >();</span><span class="pln" > wifis </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >String</span><span class="pun" >[</span><span class="pln" >wifiScanList</span><span class="pun" >.</span><span class="pln" >size</span><span class="pun" >()];</span><span class="pln" > </span><span class="kwd" >for</span><span class="pun" >(</span><span class="kwd" >int</span><span class="pln" > i </span><span class="pun" >=</span><span class="pln" > </span><span class="lit" >0</span><span class="pun" >;</span><span class="pln" > i </span><span class="pun" >&lt;</span><span class="pln" > wifiScanList</span><span class="pun" >.</span><span class="pln" >size</span><span class="pun" >();</span><span class="pln" > i</span><span class="pun" >++){</span><span class="pln" > wifis</span><span class="pun" >[</span><span class="pln" >i</span><span class="pun" >]</span><span class="pln" > </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >((</span><span class="pln" >wifiScanList</span><span class="pun" >.</span><span class="kwd" >get</span><span class="pun" >(</span><span class="pln" >i</span><span class="pun" >)).</span><span class="pln" >toString</span><span class="pun" >());</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > lv</span><span class="pun" >.</span><span class="pln" >setAdapter</span><span class="pun" >(</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >ArrayAdapter</span><span class="pun" >&lt;</span><span class="typ" >String</span><span class="pun" >&gt;(</span><span class="pln" >getApplicationContext</span><span class="pun" >(),</span><span class="pln" >android</span><span class="pun" >.</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >layout</span><span class="pun" >.</span><span class="pln" >simple_list_item_1</span><span class="pun" >,</span><span class="pln" >wifis</span><span class="pun" >));</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span></pre> <p >Following is the modified content of the xml<b >res/layout/activity_main.xml</b>.</p> <pre class="prettyprint notranslate prettyprinted" > <span class="tag" >&lt;RelativeLayout</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/apk/res/android&quot;</span><span class="pln" > </span><span class="atn" >xmlns:tools</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/tools&quot;</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;match_parent&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;match_parent&quot;</span><span class="pln" > </span><span class="atn" >android:paddingLeft</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_horizontal_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingRight</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_horizontal_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingTop</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_vertical_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingBottom</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_vertical_margin&quot;</span><span class="pln" > </span><span class="atn" >tools:context</span><span class="pun" >=</span><span class="atv" >&quot;.MainActivity&quot;</span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;TextView</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Wifi&quot;</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textview&quot;</span><span class="pln" > </span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >&quot;35dp&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignParentTop</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;TextView</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Tutorials point&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textview&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:textColor</span><span class="pun" >=</span><span class="atv" >&quot;#ff7aff24&quot;</span><span class="pln" > </span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >&quot;35dp&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;mageView</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/imageView&quot;</span><span class="pln" > </span><span class="atn" >android:src</span><span class="pun" >=</span><span class="atv" >&quot;@drawable/abc&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;ListView</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/listView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/imageView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignParentLeft</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignParentStart</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:background</span><span class="pun" >=</span><span class="atv" >&quot;#fff5d376&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;/RelativeLayout&gt;</span></pre> <p >Following is the content of<b >AndroidManifest.xml</b>file.</p> <pre class="prettyprint notranslate prettyprinted" > <span class="pun" >&lt;?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >&quot;1.0&quot;</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >&quot;utf-8&quot;</span><span class="pun" >?&gt;</span><span class="pln" > </span><span class="tag" >&lt;manifest</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/apk/res/android&quot;</span><span class="pln" > </span><span class="atn" >package</span><span class="pun" >=</span><span class="atv" >&quot;com.example.sairamkrishna.myapplication&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;uses-permission</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;android.permission.ACCESS_WIFI_STATE&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;uses-permission</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;android.permission.CHANGE_WIFI_STATE&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;application</span><span class="pln" > </span><span class="atn" >android:allowBackup</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:icon</span><span class="pun" >=</span><span class="atv" >&quot;@mipmap/ic_launcher&quot;</span><span class="pln" > </span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >&quot;@string/app_name&quot;</span><span class="pln" > </span><span class="atn" >android:theme</span><span class="pun" >=</span><span class="atv" >&quot;@style/AppTheme&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;activity</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;.MainActivity&quot;</span><span class="pln" > </span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >&quot;@string/app_name&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;action</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;android.intent.action.MAIN&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;category</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;android.intent.category.LAUNCHER&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;/intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;/activity&gt;</span><span class="pln" > </span><span class="tag" >&lt;/application&gt;</span><span class="pln" > </span><span class="tag" >&lt;/manifest&gt;</span></pre> <p >Let&#39;s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Android studio, open one of your project&#39;s activity files and click Run<img alt="Eclipse Run Icon" class="inline" src="http://www.tutorialspoint.com/android/images/eclipse_run.jpg" />icon from the toolbar. Before starting your application, Android studio will display following window to select an option where you want to run your Android application.</p> <p><img alt="Anroid Wi-Fi Tutorial" src="http://www.tutorialspoint.com/android/images/choose_device.jpg" /></p> <p >Select your mobile device as an option and then check your mobile device which will display your mobile screen filled with wireless networks around you. It is shown below &minus;</p> <p><img alt="Anroid Wi-Fi Tutorial" src="http://www.tutorialspoint.com/android/images/wifi.jpg" /></p> <p >Note the information that has been returned to you. It contains much information about each of the wireless network detected.</p> <p><img alt="android_wifi_tutorials" src="http://www.tutorialspoint.com/android/images/wifi1.jpg" /></p> <p >Above image shows different wifi connections which are connected with your devices</p>
RangeTime:0.007305s
RangeMem:239.55 KB
返回顶部 留言