Android獲取周圍WIFI熱點(diǎn)服務(wù)
在實(shí)際開(kāi)發(fā)中我們會(huì)經(jīng)常需要獲取周圍WiFi熱點(diǎn),最近做了這個(gè)demo,寫出來(lái)和大家一起分享一下吧。大體思路呢是這樣的,首先WifiManger獲取WiFi服務(wù),然后將結(jié)果儲(chǔ)存在ArrayList<ScanResult>中,好了,具體內(nèi)容大家可以直接看代碼:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
ArrayList<ScanResult> list; //存放周圍wifi熱點(diǎn)對(duì)象的列表
WifiManager wifiManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); //獲得系統(tǒng)wifi服務(wù)
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("信號(hào)最強(qiáng)為"+list.get(0).SSID);
tv2.setText("信號(hào)第二位:"+list.get(1).SSID);
tv3.setText("共有"+list.size()+"個(gè)wifi");
}
}
//將搜索到的wifi根據(jù)信號(hào)強(qiáng)度從強(qiáng)到弱進(jìn)行排序
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屬性即為強(qiáng)度
{
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,就是這個(gè)樣子吧。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決Android 6.0獲取wifi Mac地址為02:00:00:00:00:00問(wèn)題
- Android開(kāi)發(fā)實(shí)現(xiàn)在Wifi下獲取本地IP地址的方法
- Android編程獲取Wifi名稱(SSID)的方法
- Android獲取當(dāng)前手機(jī)網(wǎng)絡(luò)類型(2g、3g、4g、wifi)以及手機(jī)型號(hào)、版本號(hào)代碼
- Android編程實(shí)現(xiàn)獲取當(dāng)前連接wifi名字的方法
- Android獲取當(dāng)前已連接的wifi信號(hào)強(qiáng)度的方法
- android連接wifi時(shí)獲取廣播地址代碼
- Android獲取wifi列表的方法
相關(guān)文章
Android圖片實(shí)現(xiàn)壓縮處理的實(shí)例代碼
本篇文章主要介紹了Android圖片實(shí)現(xiàn)壓縮處理的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
SpringBoot實(shí)現(xiàn)短信驗(yàn)證碼登錄功能(案例)
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)短信驗(yàn)證碼登錄功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-08-08
Android自定義View實(shí)現(xiàn)加載進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)加載進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
淺談Android IPC機(jī)制之Binder的工作機(jī)制
IPC機(jī)制即為跨進(jìn)程通信,是inter-Process Communication的縮寫。是指兩個(gè)進(jìn)程之間進(jìn)行通信。在說(shuō)進(jìn)程通信之前,我們的弄明白什么是線程,什么是進(jìn)程。進(jìn)程和線程是兩個(gè)截然不同的概念。本文將介紹Android IPC機(jī)制之Binder的工作機(jī)制。2021-06-06
android 仿微信demo——微信消息界面實(shí)現(xiàn)(服務(wù)端)
本系列文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望能給你們提供幫助2021-06-06
Android使用RecyclerView實(shí)現(xiàn)自定義列表、點(diǎn)擊事件以及下拉刷新
這篇文章主要介紹了Android使用RecyclerView實(shí)現(xiàn)自定義列表、點(diǎn)擊事件以及下拉刷新的相關(guān)資料,需要的朋友可以參考下2016-04-04
Android編程操作嵌入式關(guān)系型SQLite數(shù)據(jù)庫(kù)實(shí)例詳解
這篇文章主要介紹了Android編程操作嵌入式關(guān)系型SQLite數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android操作SQLite數(shù)據(jù)庫(kù)的基本技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01

