标签:
<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=".WifiListActivity" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </RelativeLayout>
2.ListView的item布局文件item_wifi_list.xml内容如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/imageView" android:layout_marginBottom="14dp" android:layout_toRightOf="@+id/imageView" android:text="TextView" /> <TextView 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" /> </RelativeLayout>
3.下面就activity的代码了,这里需要自己自定义列表
public class WifiListActivity extends Activity { private WifiManager wifiManager; List<ScanResult> 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<ScanResult> list; public MyAdapter(Context context, List<ScanResult> 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; } } }
程序运行效果图如下:
标签:
原文地址:http://www.cnblogs.com/zhujiabin/p/5141615.html