这是我的广播接收器类。我正在尝试制作一个wifi扫描仪应用程序。在我的应用程序中,我将在回收视图中显示所有可用的wifi,并使用密码连接到任何网络,然后忘记网络,并在android studio中查看所有已连接网络的信息:
package com.example.mywificonnect;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pManager;
import android.util.Log;
import android.widget.Toast;
import java.util.List;
public class WifiScanner extends BroadcastReceiver {
private static final String TAG = "WiFiScanReceiver";
MainActivity mainActivity;
// ScanResult scanResult;
WifiManager wifiManagerer;
public WifiScanner(MainActivity mainActivity) {
// super();
this.mainActivity = mainActivity;
}
@Override
public void onReceive(Context context, Intent intent) {
/* String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action))*/
// wifiManager = (WifiManager) mainActivity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
List<ScanResultresults = mainActivity.wifiManager.getScanResults();
// int size=results.size();
/*for (ScanResult result: results){
Toast.makeText(mainActivity,result.SSID+""+result.level,Toast.LENGTH_SHORT).show();
}*/
ScanResult bestSignal = null;
for (ScanResult scanResult : results) {
if (bestSignal == null
|| WifiManager.compareSignalLevel(bestSignal.level, scanResult.level) < 0)
bestSignal = scanResult;
}
String message = String.format("%s networks found. %s is the strongest.",
results.size(), bestSignal.SSID);
Toast.makeText(mainActivity,message,Toast.LENGTH_LONG).show();
// if (size==0){
// Toast.makeText(mainActivity,"Size Khali Hai",Toast.LENGTH_LONG).show();
// }
/*else {
Toast.makeText(mainActivity, size, Toast.LENGTH_LONG).show();
}*/
Log.d(TAG, "onReceive message: " + message);
}
}
这是我的主要活动代码。显示所有wifi列表,然后在recyclerview中显示列表。
package com.example.mywificonnect;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "WiFiDemo";
public WifiManager wifiManager;
public BroadcastReceiver broadcastReceiver;
private TextView tvStatus;
private Button btnScan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvStatus = findViewById(R.id.tvStatus);
btnScan = findViewById(R.id.btnScan);
btnScan.setOnClickListener(this);
setLocationPermission();
setWifiStatus();
}
private void setWifiStatus() {
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
tvStatus.append("\n\nWifi Status: " + info.toString());
List<WifiConfigurationconfigurations = wifiManager.getConfiguredNetworks();
for (WifiConfiguration wifiConfiguration : configurations) {
tvStatus.append("\n\n" + wifiConfiguration.toString());
}
if (broadcastReceiver == null)
broadcastReceiver = new WifiScanner(this);
registerReceiver(broadcastReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Log.d(TAG, "onCreate()");
}
private void setLocationPermission() {
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}, 1001);
}
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(broadcastReceiver);
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(broadcastReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
@Override
public void onClick(View v) {
Toast.makeText(this, "On Click Clicked. Toast to That!!!", Toast.LENGTH_LONG).show();
if (v.getId() == R.id.btnScan) {
Log.d(TAG, "onClick() wifi.startScan()");
wifiManager.startScan();
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!