我正在尝试显示范围内所有 wifi AP 的列表,即包括具有相同 SSID 的那些,因为这些未显示在默认 wifi 设置中。
这是我对 Android SDK 的第一次尝试,到目前为止我已经做到了:
package com.iiitd.wifistats;
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.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WiFiStats extends Activity implements View.OnClickListener {
WifiManager wifi;
List<ScanResult> scanResults;
ArrayList<Map<String, Integer>> list;
SimpleAdapter adapter;
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (!wifi.isWifiEnabled()) {
Toast.makeText(getApplicationContext(), "Turning on WiFi...", Toast.LENGTH_LONG).show();
wifi.setWifiEnabled(true);
}
carryON();
}
private void carryON(){
final Button button = (Button) findViewById(R.id.buttonScan);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
wifi = (WifiManager) v.getContext().getSystemService(Context.WIFI_SERVICE);
scanResults = wifi.getScanResults();
Toast.makeText(getApplicationContext(), "Scanning...", Toast.LENGTH_SHORT).show();
list = buildData(scanResults);
//Toast.makeText(getApplicationContext(), list.toString(), Toast.LENGTH_SHORT).show();
adapter = new SimpleAdapter(v.getContext(), list, R.layout.listitem, new String[]{"BSSID", "strength"}, new int[] {R.id.BSSID, R.id.strength});
//Toast.makeText(getApplicationContext(), adapter.toString(), Toast.LENGTH_SHORT).show();
listview = (ListView) findViewById(R.id.list);
listview.setAdapter(adapter);
}
});
}
private ArrayList<Map<String, Integer>> buildData(java.util.List<ScanResult> s) {
ArrayList<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
for (ScanResult result : s) {
list.add(putData(result.BSSID, result.level));
}
return list;
}
private HashMap<String, Integer> putData(String BSSID, int level) {
HashMap<String, Integer> item = new HashMap<String, Integer>();
item.put(BSSID, level);
return item;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.wi_fi_stats, menu);
return true;
}
@Override
public void onClick(View view) {
}
}
这是我的activity_xml:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan"
android:id="@+id/buttonScan"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"
android:layout_above="@+id/buttonScan"
android:layout_alignParentLeft="true"
android:layout_marginBottom="14dp"/>
</RelativeLayout>
这是 listitem.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<TextView
android:id="@+id/strength"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp"/>
<TextView
android:id="@+id/BSSID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/strength"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp"/>
</RelativeLayout>
当我开始扫描时没有显示列表。但是,如果我使用 toast,我可以看到列表正确形成。有人可以在这里指导我吗?谢谢。