android:id="@+id/signal_strenth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView"
android:layout_alignBottom="@+id/textView"
android:layout_alignParentRight="true"
android:text="TextView" />
3.下面就activity的代码了,这里需要自己自定义列表。
[java]
public class WifiListActivity extends Activity {
private WifiManager wifiManager;
List list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifi_list);
init();
}
private void init() {
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
openWifi();
list = wifiManager.getScanResults();
ListView listView = (ListView) findViewById(R.id.listView);
if (list == null) {
Toast.makeText(this, "wifi未打开!", Toast.LENGTH_LONG).show();
}else {
listView.setAdapter(new MyAdapter(this,list));
}
}
/**
* 打开WIFI
*/
private void openWifi() {
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
}
public class MyAdapter extends BaseAdapter {
LayoutInflater inflater;
List list;
public MyAdapter(Context context, List list) {
// TODO Auto-generated constructor stub
this.inflater = LayoutInflater.from(context);
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = null;
view = inflater.inflate(R.layout.item_wifi_list, null);
ScanResult scanResult = list.get(position);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(scanResult.SSID);
TextView signalStrenth = (TextView) view.findViewById(R.id.signal_strenth);
signalStrenth.setText(String.valueOf(Math.abs(scanResult.level)));
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
//判断信号强度,显示对应的指示图标
if (Math.abs(scanResult.level) > 100) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_0));
} else if (Math.abs(scanResult.level) > 80) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_1));
} else if (Math.abs(scanResult.level) > 70) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_1));
} else if (Math.abs(scanResult.level) > 60) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_2));
} else if (Math.abs(scanResult.level) > 50) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_3));
} else {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_4));
}
return view;
}
}
}
程序运行效果图如下: