我是android studio的新手,想知道是否有人知道如何获取用户的Mac地址并将其显示在带有标签lbl_Mac的文本视图中?
这是我的文本视图xml
<TextView
android:id="@+id/lbl_Mac"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="MAC Address: Not Found"
android:textColor="#FFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.989"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.983" />
我想使用此代码获取MAC地址:
public String getMacAddress(Context context) {
WifiManager wimanager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
String macAddress = wimanager.getConnectionInfo().getMacAddress();
if (macAddress == null) {
macAddress = "Device don't have mac address or wi-fi is disabled";
}
return macAddress;
}
但我想将mac地址设置为文本视图。
只需要将公共字符串名称添加到setText括号中。
lbl_Mac.setText(***getMacAddress***(this));
例如
public String ***getMacAddress***(Context context) {
WifiManager wimanager = (WifiManager)
context.getSystemService(Context.WIFI_SERVICE);
String macAddress = wimanager.getConnectionInfo().getMacAddress();
if (macAddress == null) {
macAddress = "Device don't have mac address or wi-fi is disabled";
}
return macAddress;