Android-判断某个网络是否在网络列表中

此功能主要用的知识点有下:

WifiManager类,获取对象的方法如下:

mWifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

通过调用这个对象的getScanResults方法,可以获取当前扫描到的网络列表List,即:List<ScanResult>。

CountDownTimer类(这个类在倒计时的Button中也曾用到),此类是抽象类,继承这个类要实现两个方法:

onTick方法,倒计时的过程中会不断调用,里面的参数表示距离倒计时结束的时间。

onFinish方法,倒计时结束时调用。

布局代码如下:

<?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">
   <Button
       
android:id="@+id/btn"
       
android:onClick="isWifiExist"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:text="查找网络" />
</RelativeLayout>

MainActivity.java代码如下:

public class MainActivity extends Activity {
   public static final String TAG = "YAYUN";
   private static
WifiManager mWifiManager;
   static boolean
isCanScan = false;
   private
TimeCount timeCount;
   private
ProgressDialog progress;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       
setContentView(R.layout.activity_main);
       
mWifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
       
timeCount = new TimeCount(20 * 1000, 1000);//实例化TimeCount类
       // 20*1000表示总倒计时长为20秒,调用间隔为1秒。
       
progress = new ProgressDialog(this);
   
}
   public void isWifiExist(View view) {
           progress.setTitle("正在搜索...");
       
progress.show();
       
timeCount.start();//调用start方法开始倒计时
   
}
   class TimeCount extends CountDownTimer {
       //构造方法
       
public TimeCount(long totalTime, long interval) {
           super(totalTime, interval);
       
}
       @Override
       
public void onTick(long millisUntilFinished) {//覆写方法-计时中
           
if (isWifiCanScan("iPhone")) {
                       progress.dismiss();//隐藏进度框
               
timeCount.cancel();//取消倒计时
               
MainActivity.mButton.setText("已找到该网络");
           
}
       }
       @Override
       
public void onFinish() {//覆写方法,即时结束
           
MainActivity.mButton.setText("找不到该网络");
           
progress.dismiss();
           
isCanScan = false;
       
}
   }
   public boolean isWifiCanScan(String SSID) {
       boolean isExist = false;
       
List<ScanResult> mWifiList = mWifiManager.getScanResults();
       if
(mWifiList == null) {
           return false;
       
}
       for (ScanResult existingWifi : mWifiList) {
           if (existingWifi.SSID.equals(SSID)) {
               isExist = true;
               return
isExist;
           
}
       }
       return false;
   
}
}

isWifiCanScan方法判断扫描出的网络列表中是否包含指定的某个网络,首先通过getScanResult方法获取所有扫描到的网络列表,然后对网络列表进行遍历,通过SSID进行判断。若找到了就返回true,找不到则返回false。

设置了倒计时时间为20秒,每隔一秒钟调用一次方法判断是否找到了该网络,若找到了则隐藏进度框并调用CountDownTimer类的cancel方法取消倒计时。若找不到也将隐藏进度框并设置Button的text为“找不到该网络”。

运行实例如下:


打开名为“iPhone”的热点,再点击“查找网络”按钮,显示如下:

谢谢关注我的微信公众号,觉得好可以分享到朋友圈哦.

 

 

请关注我的新浪微博:AndroidTip

 

CSDN博客:http://blog.csdn.net/yayun0516

 

发布了498 篇原创文章 · 获赞 115 · 访问量 126万+

猜你喜欢

转载自blog.csdn.net/yayun0516/article/details/52269408