直接上源码了,如果有问题欢迎留言一起讨论。
</pre><pre name="code" class="java">1、以下是activity中的代码
public class MainActivity extends Activity implements OnClickListener {
private WifiManager wifiManager;
private List<String> oldDatas;//存储的数据
private List<ScanResult> listdatas2;//需要存储的数据
private Button refresh_btn;//刷新按钮
private ListView listView;
private MyAdapter myadapter;
private Button save_btn;//保存按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
init();
}
private void init() {
listView = (ListView) findViewById(R.id.show_wifi_list);
refresh_btn = (Button) findViewById(R.id.refresh_btn);
refresh_btn.setOnClickListener(this);
save_btn = (Button) findViewById(R.id.save_btn);
save_btn.setOnClickListener(this);
myadapter = new MyAdapter(this);
openWifi();
List<ScanResult> listdatas1 = wifiManager.getScanResults();
listView.setAdapter(myadapter);
refreshList(listdatas1);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.refresh_btn:
save_btn.setEnabled(true);
List<ScanResult> lists1 = wifiManager.getScanResults();
refreshList(lists1);
break;
case R.id.save_btn:
if(wifiManager.getScanResults().size()>0){
saveWifiHot();
}else{
Toast.makeText(getApplication(), "没有数据需要保存", 1).show();// 保存成功
}
break;
default:
break;
}
}
/**
* 刷新数据
* @param lists1
*/
private void refreshList(List<ScanResult> lists1){
listdatas2 = new ArrayList<ScanResult>();
String id = "";
for (int i = 0; i < lists1.size(); i++) {
//去掉相同ssid的
if (!id.equals(lists1.get(i).SSID)
&& !(lists1.get(i).SSID.equals(""))) {
id = lists1.get(i).SSID;
listdatas2.add(lists1.get(i));
}
}
myadapter.setList(listdatas2);
myadapter.notifyDataSetChanged();
}
/**
* 保存到sdcard的wifihot文本中
*/
private void saveWifiHot() {
try {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
saveToSDCard("wifihot");// 保存数据到SDCard
Toast.makeText(getApplication(), "保存成功", 1).show();// 保存成功
save_btn.setEnabled(false);
} else {
save_btn.setEnabled(true);
Toast.makeText(getApplication(), "保存失败", 1).show();// 保存失败
}
} catch (Exception e) {
save_btn.setEnabled(true);
Toast.makeText(getApplication(), "保存失败", 1).show();
e.printStackTrace();
}
}
// 保存数据到SDCard
public void saveToSDCard(String filename) throws Exception {
// 得到文件对象,找到SDCard的路径
// File file = new File("mnt/sdcard", filename);
File file = new File(Environment.getExternalStorageDirectory(),
filename);
String shareData;
//文件存在则读取文件中的数据,不存在则创建新的文件再写入数据
if(file.exists()){
oldDatas = readSDCardData(filename);
}else{
oldDatas=new ArrayList<String>();
}
for (int i = 0; i < listdatas2.size(); i++) {
shareData = "wifiName:" + listdatas2.get(i).SSID;
oldDatas.add(shareData);
}
// 得到文件输出流对象
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(oldDatas.toString().getBytes());
outStream.close();
}
// 读取SDCard文件中的数据
public List<String> readSDCardData(String filename) throws IOException {
File file = new File(Environment.getExternalStorageDirectory(),
filename);
List<String> oldDatas = new ArrayList<String>();
FileInputStream in = new FileInputStream(file);
byte[] a = new byte[in.available()];
in.read(a);
oldDatas.add((new String(a)).substring(1, (new String(a)).length()));
in.close();
// Toast.makeText(getApplication(), oldDatas.toString(), 1).show();
return oldDatas;
}
/**
* 打开WIFI
*/
private void openWifi() {
if (!wifiManager.isWifiEnabled()) {
Toast.makeText(this, "正在为您打开Wifi!,请稍等!!!", Toast.LENGTH_LONG).show();
wifiManager.setWifiEnabled(true);
}
}
public class MyAdapter extends BaseAdapter {
LayoutInflater inflater;
List<ScanResult> list;
public MyAdapter(Context context) {
this.inflater = LayoutInflater.from(context);
}
public void setList(List<ScanResult> list) {
this.list = list;
}
@Override
public int getCount() {
return list == null ? 0 : list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
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.wifi_list_item, 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);
return view;
}
}
}
2、xml里面的代码
<LinearLayout 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:orientation="vertical">
<Button android:id="@+id/refresh_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="刷新"/>
<Button android:id="@+id/save_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="保存"/>
<ListView
android:id="@+id/show_wifi_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
3、listview item中的代码
<?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"
android:orientation="vertical" >
<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>