AndroidManifest.xml,增加2个权限声明
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.wifiscanner"> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
activity_main.xml,增加一个
LinearLayout和ListView
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/list"></ListView> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.wifiscanner; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.Manifest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.net.wifi.ScanResult; import android.net.wifi.WifiManager; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.ListView; import java.sql.Time; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { private List<String> wifilist = new ArrayList<>(); private ListView listView; private WifiReceiver wifiReceiver; private ArrayAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.list); // requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_WIFI_STATE},0x123); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission( Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[] { Manifest.permission.ACCESS_COARSE_LOCATION ,Manifest.permission.ACCESS_WIFI_STATE},0x123); } else { getWifiList(); adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,wifilist); listView.setAdapter(adapter); } IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); wifiReceiver = new WifiReceiver(); registerReceiver(wifiReceiver,intentFilter); final getWifilistHandler handler = new getWifilistHandler(); new Timer().schedule(new TimerTask() { @Override public void run() { handler.sendEmptyMessage(0x123); } },0,1000); } public class getWifilistHandler extends Handler{ @Override public void handleMessage(@NonNull Message msg) { if(msg.what == 0x123) { getWifiList(); adapter.notifyDataSetChanged(); } super.handleMessage(msg); } } public class WifiReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { getWifiList(); adapter.notifyDataSetChanged(); // if (wifilist != null){ // ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,wifilist); // listView.setAdapter(adapter); // } } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (0x123 == requestCode && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) { getWifiList(); adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,wifilist); listView.setAdapter(adapter); } // super.onRequestPermissionsResult(requestCode, permissions, grantResults); } public List<String> getWifiList() { WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); List<ScanResult> scanWifiList = wifiManager.getScanResults(); wifilist.clear(); if (scanWifiList != null && scanWifiList.size() > 0) { HashMap<String, Integer> signalStrength = new HashMap<String, Integer>(); for (int i = 0; i < scanWifiList.size(); i++) { ScanResult scanResult = scanWifiList.get(i); if (!scanResult.SSID.isEmpty()) { String key = scanResult.SSID + " " + scanResult.capabilities; if (!signalStrength.containsKey(key)) { signalStrength.put(key, i); wifilist.add(scanResult.SSID+" "+ scanResult.BSSID+" " +String.valueOf(scanResult.level)); } } } }else { Log.e("Msg__---", "没有搜索到wifi"); } return wifilist; } }
开发中遇到一个问题,是ListView没有更新,调试发现apapter中数据已经更新,网上查找说要增加
adapter.notifyDataSetChanged();
增加后,依然显示不更新,最后检查发现adapter绑定的list地址发生了变化,导致更新界面出现了问题,最后修改了,可以正常更新
使用小米3,Android6.0,华为Mate10pro,Android9.0,Android Studio3.5,测试,小米3可以及时刷新,华为不行,要有段时间才能刷新,原因待查