gpt4 book ai didi

android - 如何连接到 ListView 中可用的 wi-fi

转载 作者:行者123 更新时间:2023-11-29 17:46:52 25 4
gpt4 key购买 nike

我想让用户连接到wi-fi列表中可用的wi-fi。我想显示一个对话框警报,如果它的 protected 和其他设置与我们打开 wi-fi 并点击可用的 wi-fi 时显示的 android 手机相同,则在点击 wi-fi 时询问密码。

到目前为止,我所做的是在 ListView 中获取可用 Wi-Fi 的列表。

这是我的代码。

import java.util.List;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
WifiManager mainWifiObj;
WifiScanReceiver wifiReciever;
ListView list;
String wifis[];


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.listView1);
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WifiScanReceiver();
mainWifiObj.startScan();

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

protected void onResume() {
registerReceiver(wifiReciever, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
wifis = new String[wifiScanList.size()];
for(int i = 0; i < wifiScanList.size(); i++){
wifis[i] = ((wifiScanList.get(i)).toString());
}
String filtered[] = new String[wifiScanList.size()];
int counter = 0;
for (String eachWifi : wifis) {
String[] temp = eachWifi.split(",");

filtered[counter] = temp[0].substring(5).trim()+"\n" + temp[2].substring(12).trim()+"\n" +temp[3].substring(6).trim();//0->SSID, 2->Key Management 3-> Strength


counter++;

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


}
}
}

最佳答案

这是我的 WiFi 连接代码,请看一下,希望对您有所帮助:

    import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
private WifiManager wifi;
private ListView lv;
// TextView textStatus;
private Button buttonScan;
private int size = 0;
private List<ScanResult> results;
private final Context context = this;
private EditText pass;
private String checkPassword = null;

private String ITEM_KEY = "key";
private ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
private SimpleAdapter adapter;
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// textStatus = (TextView) findViewById(R.id.textStatus);
buttonScan = (Button) findViewById(R.id.buttonScan);
buttonScan.setOnClickListener(this);
lv = (ListView) findViewById(R.id.list);
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
connectToWifi(arg2);
}

});

lv.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
showWifiSettings(arg2);
return true;
}
});

if (wifi.isWifiEnabled() == false) {
Toast.makeText(getApplicationContext(),
"wifi is disabled..making it enabled", Toast.LENGTH_LONG)
.show();
wifi.setWifiEnabled(true);
}

adapter = new SimpleAdapter(MainActivity.this, arraylist,
R.layout.row, new String[] { ITEM_KEY },
new int[] { R.id.list_value });
lv.setAdapter(adapter);

registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
results = wifi.getScanResults();
size = results.size();
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}

@Override
public void onClick(View view) {

arraylist.clear();
wifi.startScan();

Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show();
try {
size = size - 1;
while (size >= 0) {
HashMap<String, String> item = new HashMap<String, String>();
item.put(ITEM_KEY,
results.get(size).SSID.toString()
+ results.get(size).capabilities.toString());

arraylist.add(item);
size--;
adapter.notifyDataSetChanged();
}

} catch (Exception e) {
}
}

private void showWifiSettings(int arg2) {
showDialogOfOptions(arg2);
}

private void showDialogOfOptions(int arg2) {
// Create a custom Dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Network details");
TextView textSSID = (TextView) dialog.findViewById(R.id.textSSID);
TextView textBSSID = (TextView) dialog.findViewById(R.id.textBSSID);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
textSSID.setText(results.get(arg2).SSID);
textBSSID.setText(results.get(arg2).BSSID);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}

private void finallyConnect(String checkPassword, int position) {
String networkSSID = results.get(position).SSID;
String networkPass = checkPassword;

WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", networkSSID);
wifiConfig.preSharedKey = String.format("\"%s\"", networkPass);

// remember id
int netId = wifi.addNetwork(wifiConfig);
wifi.disconnect();
wifi.enableNetwork(netId, true);
wifi.reconnect();

WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"\"" + networkSSID + "\"\"";
conf.preSharedKey = "\"" + networkPass + "\"";
wifi.addNetwork(conf);
}

private void connectToWifi(final int position) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.connect);
dialog.setTitle("Connect to Network");
TextView textSSID = (TextView) dialog.findViewById(R.id.textSSID1);
TextView textBSSID = (TextView) dialog.findViewById(R.id.textBSSID1);
TextView capabilities = (TextView) dialog
.findViewById(R.id.textCapabilities);

Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
pass = (EditText) dialog.findViewById(R.id.textPassword);
textSSID.setText(results.get(position).SSID);
textBSSID.setText(results.get(position).BSSID);
capabilities.setText(results.get(position).capabilities);
//
// if button is clicked, connect to the network;
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
checkPassword = pass.getText().toString();
finallyConnect(checkPassword, position);
dialog.dismiss();
}
});
dialog.show();
}
}

关于android - 如何连接到 ListView 中可用的 wi-fi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25930149/

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