编程实现 Android 手机上 WiFi 操作,要求有java文件和xml布局文件
时间: 2023-12-03 22:43:12 浏览: 109
以下是一个简单的 Android WiFi 操作的示例,包括一个Java文件和一个XML布局文件。
1. Java文件
```java
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private WifiManager wifiManager;
private EditText wifiNameEditText, wifiPasswordEditText;
private TextView wifiStatusTextView, wifiInfoTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiNameEditText = findViewById(R.id.wifiNameEditText);
wifiPasswordEditText = findViewById(R.id.wifiPasswordEditText);
wifiStatusTextView = findViewById(R.id.wifiStatusTextView);
wifiInfoTextView = findViewById(R.id.wifiInfoTextView);
Button connectButton = findViewById(R.id.connectButton);
connectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String wifiName = wifiNameEditText.getText().toString();
String wifiPassword = wifiPasswordEditText.getText().toString();
connectToWifi(wifiName, wifiPassword);
}
});
Button disconnectButton = findViewById(R.id.disconnectButton);
disconnectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
disconnectFromWifi();
}
});
Button wifiInfoButton = findViewById(R.id.wifiInfoButton);
wifiInfoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showWifiInfo();
}
});
}
private void connectToWifi(String wifiName, String wifiPassword) {
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", wifiName);
wifiConfig.preSharedKey = String.format("\"%s\"", wifiPassword);
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
wifiStatusTextView.setText("Connecting...");
}
private void disconnectFromWifi() {
wifiManager.disconnect();
wifiStatusTextView.setText("Disconnected");
}
private void showWifiInfo() {
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
wifiInfoTextView.setText("SSID: " + wifiInfo.getSSID() + "\n" +
"BSSID: " + wifiInfo.getBSSID() + "\n" +
"Speed: " + wifiInfo.getLinkSpeed() + WifiInfo.LINK_SPEED_UNITS + "\n" +
"Signal Strength: " + wifiInfo.getRssi() + " dBm" + "\n" +
"IP Address: " + intToIp(wifiInfo.getIpAddress()));
}
private String intToIp(int i) {
return ((i >> 24) & 0xFF) + "." +
((i >> 16) & 0xFF) + "." +
((i >> 8) & 0xFF) + "." +
(i & 0xFF);
}
}
```
2. XML布局文件
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/wifiNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="WiFi Name"
android:inputType="text" />
<EditText
android:id="@+id/wifiPasswordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/wifiNameEditText"
android:hint="WiFi Password"
android:inputType="textPassword" />
<Button
android:id="@+id/connectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/wifiPasswordEditText"
android:layout_marginTop="16dp"
android:text="Connect" />
<Button
android:id="@+id/disconnectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/connectButton"
android:layout_marginTop="16dp"
android:text="Disconnect" />
<Button
android:id="@+id/wifiInfoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/disconnectButton"
android:layout_marginTop="16dp"
android:text="WiFi Info" />
<TextView
android:id="@+id/wifiStatusTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/wifiInfoButton"
android:layout_marginTop="16dp"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/wifiInfoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/wifiStatusTextView"
android:layout_marginTop="16dp"
android:textSize="18sp" />
</RelativeLayout>
```
阅读全文