gpt4 book ai didi

java - Android:扫描获取 WiFi 列表 - ListView 未出现

转载 作者:行者123 更新时间:2023-12-02 12:24:18 27 4
gpt4 key购买 nike

我编写了一个程序来开发一个应用程序来显示扫描的 WIFI 接入点。我的应用程序运行时没有任何错误,但不幸的是它没有生成任何结果,尽管看起来我已经完成了所有操作。我正在附上我的所有文件,如果有人能指出我缺少的内容,我将不胜感激。干杯,伙计们!

MainActivity.java

package com.example....;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.ToggleButton;
import android.view.Menu;
import android.view.MenuItem;
import java.util.List;
import java.util.ArrayList;
import android.net.wifi.ScanResult;

public class MainActivity extends AppCompatActivity {

ToggleButton toggleButton;
TextView textView;
ListView list;
String len[];

public WifiManager wifi;
WifiReceiver receiverWifi;

StringBuilder sb = new StringBuilder();
private final Handler handler = new Handler();
private BroadcastReceiver receiver;


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

wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);


textView = (TextView) findViewById(R.id.textView);
list = (ListView)findViewById(R.id.listview);
receiverWifi = new WifiReceiver();
wifi.startScan();
registerReceiver(receiverWifi, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
if(wifi.isWifiEnabled()==false)
{
wifi.setWifiEnabled(true);
}



// #######################################################################################################################
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if (checked) {
textView.setText("WiFi is ON");
WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);
} else {
textView.setText("WiFi is OFF");
WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(false);
}
}
});

if (toggleButton.isChecked()) {
textView.setText("WiFi is ON");
WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);
} else {
textView.setText("WiFi is OFF");
WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(false);
}
// #######################################################################################################################

}



protected void onPause() {
unregisterReceiver(receiverWifi);
super.onPause();
}

protected void onResume() {
registerReceiver(receiverWifi, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}

class WifiReceiver extends BroadcastReceiver {

public void onReceive(Context c, Intent intent) {

ArrayList<String> connections=new ArrayList<String>();
ArrayList<Float> Signal_Strenth= new ArrayList<Float>();
List<ScanResult> wifiList = wifi.getScanResults();
wifiList = wifi.getScanResults();

len = new String[wifiList.size()];

for(int i = 0; i < wifiList.size(); i++){
len[i] = ((wifiList.get(i)).toString());
}

list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.activity_list_item,len));







}
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example......"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WiFi Network Status"
android:id="@+id/textView"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="16dp"
android:layout_marginTop="26dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="16dp" />

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="16dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

tools:layout_constraintTop_creator="1"
android:layout_marginStart="16dp"
android:layout_marginTop="110dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="16dp" />

<ListView
android:id="@+id/listview"
android:layout_width="0dp"
android:layout_height="0dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginStart="15dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="15dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="18dp"
app:layout_constraintTop_toBottomOf="@+id/toggleButton"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="20dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />

</android.support.constraint.ConstraintLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.....">

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

<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>

最佳答案

我发现你的问题了

更改android.R.layout.activity_list_item

list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.activity_list_item,len));

android.R.layout.simple_list_item_1

list.setAdapter(new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, len));

关于java - Android:扫描获取 WiFi 列表 - ListView 未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45560684/

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