首先,在button的XML文件中添加android:onClick属性,如下所示:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Get Mac Address"
    android:onClick="getMacAddress" />

然后,在Activity中添加getMacAddress方法,如下所示:

public void getMacAddress(View view) {
    try {
        // 获取WifiManager对象
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        // 获取WifiInfo对象
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        // 获取Mac地址
        String macAddress = wifiInfo.getMacAddress();
        // 输出Mac地址
        System.out.println("Mac Address: " + macAddress);
        Toast.makeText(this, "Mac Address: " + macAddress, Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

需要注意的是,为了获取手机的Mac地址,需要在AndroidManifest.xml文件中添加以下权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

在这个方法中,首先获取WifiManager对象,然后通过它获取WifiInfo对象,最后调用getMacAddress方法获取Mac地址。输出Mac地址的方式有两种:一种是在控制台上输出,另一种是通过Toast在屏幕上输出。

标准库:Android提供了WifiManager类和WifiInfo类,用于获取手机的Mac地址。

给AndroidStudio的button添加一个andriodclick点击以后利用JAVA的标准库获取手机mac地址并且输出在屏幕和控制台上以及你所用到的标准也要列出来

原文地址: https://www.cveoy.top/t/topic/boe9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录