AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cy.getwifi" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.cy.getwifi.GetWiFi" 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>
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".GetWiFi" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" > </ListView> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="Button" /> </RelativeLayout>
Code:
package com.cy.getwifi; import java.util.List; import android.app.Activity; import android.content.Context; import android.net.wifi.ScanResult; import android.net.wifi.WifiManager; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; public class GetWiFi extends Activity { private ArrayAdapter<String> adapter; private Button button; private ListView listView; private WifiManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_get_wi_fi); init(); } private void init() { button = (Button) findViewById(R.id.button1); listView = (ListView) findViewById(R.id.listView1); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); manager = (WifiManager) getSystemService(Context.WIFI_SERVICE); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Boolean isEnable = manager.isWifiEnabled(); if (isEnable == false) manager.setWifiEnabled(true); manager.startScan(); if (adapter.getCount() > 0) adapter.clear(); List<ScanResult> l = manager.getScanResults(); for (ScanResult s : l) { adapter.add(s.SSID); } if (adapter.getCount() > 0) listView.setAdapter(adapter); } }); } }
參考資料:
http://developer.android.com/reference/android/net/wifi/WifiManager.html
http://developer.android.com/reference/android/net/wifi/WifiManager.html#startScan()
http://developer.android.com/reference/android/net/wifi/WifiManager.html#getScanResults()
http://developer.android.com/reference/android/net/wifi/WifiManager.html#setWifiEnabled(boolean)
http://developer.android.com/reference/android/net/wifi/ScanResult.html