title: How to show list of wifi networks on button click
body: <p>I want to get list of wifi networks available on button click
  The code i used is this But i am not able to get the result and i am getting exceptions...</p>
  
  <p>in the manifest file...</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  </code></pre>
  
  <p>in the Activity class...</p>
  
  <pre><code>    public class MainActivity extends Activity {
  
      Button b;
      TextView tv;
      Context con;
  WifiManager wmg;
  @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          //b=(Button)findViewById(R.id.button1);
          tv=(TextView)findViewById(R.id.textView1);
          //b.setOnClickListener(this);
          wmg = (WifiManager)getSystemService(Context.WIFI_SERVICE);
          lv=(ListView)findViewById(R.id.listView1);
          wmg.startScan();
      }
  @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          //b=(Button)findViewById(R.id.button1);
          tv=(TextView)findViewById(R.id.textView1);
          //b.setOnClickListener(this);
          wmg = (WifiManager)getSystemService(Context.WIFI_SERVICE);
          lv=(ListView)findViewById(R.id.listView1);
          wmg.startScan();
      }
  private final BroadcastReceiver myWifiScanReceiver = new BroadcastReceiver() {
  
          @Override
          public void onReceive(Context context, Intent intent) {
              // TODO Auto-generated method stub
              if (intent.getAction() == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) {
                  List&lt;ScanResult&gt; mScanResults = wmg.getScanResults();
                  for (ScanResult result : mScanResults) {
  
                       Log.i("WIFI_NTWRKS",result.SSID);
                        }
  
                          }
  
              }
  
      };
  </code></pre>
  
  <p>It is giving exception...</p>
  
  <p>The logcat is...</p>
  
  <pre><code>03-20 16:28:55.008: D/AndroidRuntime(4959): Shutting down VM
  03-20 16:28:55.008: W/dalvikvm(4959): threadid=1: thread exiting with uncaught exception (group=0x2aac8578)
  03-20 16:28:55.008: E/AndroidRuntime(4959): FATAL EXCEPTION: main
  03-20 16:28:55.008: E/AndroidRuntime(4959): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wifi/com.example.wifi.MainActivity}: java.lang.SecurityException: WifiService: Neither user 10145 nor current process has android.permission.CHANGE_WIFI_STATE.
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.app.ActivityThread.access$1500(ActivityThread.java:121)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.os.Handler.dispatchMessage(Handler.java:99)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.os.Looper.loop(Looper.java:130)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.app.ActivityThread.main(ActivityThread.java:3701)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at java.lang.reflect.Method.invokeNative(Native Method)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at java.lang.reflect.Method.invoke(Method.java:507)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at dalvik.system.NativeStart.main(Native Method)
  03-20 16:28:55.008: E/AndroidRuntime(4959): Caused by: java.lang.SecurityException: WifiService: Neither user 10145 nor current process has android.permission.CHANGE_WIFI_STATE.
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.os.Parcel.readException(Parcel.java:1322)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.os.Parcel.readException(Parcel.java:1276)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.net.wifi.IWifiManager$Stub$Proxy.startScan(IWifiManager.java:508)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.net.wifi.WifiManager.startScan(WifiManager.java:573)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at com.example.wifi.MainActivity.onCreate(MainActivity.java:39)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
  03-20 16:28:55.008: E/AndroidRuntime(4959):     ... 11 more
  </code></pre>