且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

创建一个全局唯一标识符的Andr​​oid

更新时间:2023-02-07 14:10:53

你为什么不拿到设备的MAC地址作为你的iOS已经做了什么?这可以在Android设备以及进行。

Why don't you get MAC address of the device as you've done in iOS? This can be performed in Android devices as well.

我会给其获得设备的MAC地址的code段。

I'll give a code snippet that obtains mac address of the device..

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class HardwareUtil {

    public static String getMacAddress()
    {
        try{
            Process process = Runtime.getRuntime().exec("cat /sys/class/net/eth0/address");
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            int read;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((read = reader.read(buffer)) > 0){
                output.append(buffer, 0, read);
            }
            reader.close();
            process.waitFor();
            String hwaddr = output.toString();
            return hwaddr;
        }catch (IOException e) {
            e.printstacktrace();
        }catch (InterruptedException e){
            e.printstacktrace();
        }
    }

}

HardwareUtil.getMacAddress()将返回设备的MAC地址。

HardwareUtil.getMacAddress() will return mac address of the device.

编辑:如果MAC地址不适合您的情况。下面可能是有用的!

public static String getDeviceId(Context context) {
    final String deviceId = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
    if (deviceId != null) {
        return deviceId;
    } else {
        return android.os.Build.SERIAL;
    }
}