gpt4 book ai didi

java - Android Studio Wifi管理器

转载 作者:太空宇宙 更新时间:2023-11-04 12:28:16 25 4
gpt4 key购买 nike

我想搜索可用的 WiFi 网络,并在单击应用程序中的开始按钮时将它们显示在列表中,但我的代码从未找到任何 WiFi 网络。我尝试使用 TextViewToast 显示可用网络的数量,但始终显示 0

这是我的代码:

public class MainActivity extends AppCompatActivity {

//Listview
private ArrayAdapter<String> adapter;
private ListView list;
private ArrayList<String> mylist = new ArrayList<String>();
ListAdapter adapter1;
TextView tV;

//Wifi
private WifiManager mywifi;
WifiReceiver myreceiver;
List<ScanResult> wifiList;
StringBuilder sb;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//definieren
Button start = (Button) findViewById(R.id.btnstart);

//Listview
list = (ListView) findViewById(R.id.lV);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mylist);
list.setAdapter(adapter);

//Textview
tV = (TextView) findViewById(R.id.tV);
tV.setText("");

//Wifi
mywifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (mywifi.isWifiEnabled() == false) {
Toast.makeText(getApplicationContext(), "Wifi is disabled ....making it enabled", Toast.LENGTH_SHORT).show();
mywifi.setWifiEnabled(true);
}
myreceiver = new WifiReceiver();
registerReceiver(myreceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mywifi.startScan();

//buttenevents
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//getWifiNetworksList();
}
});

}

public void getWifiNetworksList() {
tV.setText("Wifi is starting");
tV.setText(myreceiver.Wifilist());

}

class WifiReceiver extends BroadcastReceiver {

public String txt;

// This method call when number of wifi connections changed
public void onReceive(Context c, Intent intent) {

sb = new StringBuilder();
wifiList = mywifi.getScanResults();
sb.append("\n Number Of Wifi connections :" + wifiList.size() + "\n\n");

for (int i = 0; i < wifiList.size(); i++) {

sb.append(new Integer(i + 1).toString() + ". ");
sb.append((wifiList.get(i)).toString());
sb.append("\n\n");
}

tV.setText(sb);//tV...TextView
Toast.makeText(getApplicationContext(), "reciever finished", Toast.LENGTH_SHORT).show();
}

public String Wifilist() {
return txt;
}

}
}

最佳答案

启用位置权限。

代码工作得非常好。在 Nexus 5 上测试。我刚刚在 list 文件中添加了以下权限

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

如果仍然没有显示任何结果。只需确保您的用户已向您的应用授予位置权限即可。
您可以通过两种方式执行此操作

Manually

前往手机设置并点击该应用的启用选项。 (在我的例子中,我的应用程序名称是 WifiSolution)。

enter image description here

Programmatically

您还可以使用Android Runtime Permission以编程方式执行此操作。

关于java - Android Studio Wifi管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38152297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com