
在实际开发中我们会经常需要获取周围WiFi热点,最近做了这个demo,写出来和大家一起分享一下吧。大体思路呢是这样的,首先Wifimanger获取WiFi服务,然后将结果储存在ArrayList<ScanResult>中,好了,具体内容大家可以直接看代码:
MainActivity.java:
public class MainActivity extends AppCompatActivity { ArrayList<ScanResult> List; //存放周围wifi热点对象的列表 WifiManager wifiManager; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); //获得系统wifi服务 List = (ArrayList<ScanResult>)wifiManager.getScanResults(); sortByLevel(List); init(); } private voID init(){ TextVIEw tv1=(TextVIEw)findVIEwByID(R.ID.tv1); TextVIEw tv2=(TextVIEw)findVIEwByID(R.ID.tv2); TextVIEw tv3=(TextVIEw)findVIEwByID(R.ID.tv3); if (List.get(0).SSID != null && List.get(1).SSID != null){ tv1.setText("信号最强为"+List.get(0).SSID); tv2.setText("信号第二位:"+List.get(1).SSID); tv3.setText("共有"+List.size()+"个wifi"); } } //将搜索到的wifi根据信号强度从强到弱进行排序 private voID sortByLevel(ArrayList<ScanResult> List) { for(int i=0;i<List.size();i++) for(int j=1;j<List.size();j++) { if(List.get(i).level<List.get(j).level) //level属性即为强度 { ScanResult temp = null; temp = List.get(i); List.set(i,List.get(j)); List.set(j,temp); } } }} 布局文件activity_main.xml:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical"> <TextVIEw androID:ID="@+ID/tv1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Hello World!" /> <TextVIEw androID:ID="@+ID/tv2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Hello World!" /> <TextVIEw androID:ID="@+ID/tv3" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Hello World!" /></linearLayout>
OK,就是这个样子吧。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
您可能感兴趣的文章:Android便携式热点的开启状态检测和SSID的获取方法Android 连接Wifi和创建Wifi热点的实例android编程实现设置、打开wifi热点共享供他人连接的方法android多行标签热点示例Android获取实时连接热点的设备IP 总结以上是内存溢出为你收集整理的Android获取周围WIFI热点服务全部内容,希望文章能够帮你解决Android获取周围WIFI热点服务所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)