When developing Android applications, we often need a unique device code to determine the client.
Several Methods in Android are often unreliable in use
1. DEVICE_ID
If we do need the identifier of a real device, DEVICE_ID may be used. Obtained Through TelephonyManager. getDeviceId (). It returns IMEI, MEID, or ESN code based on different mobile devices.
Disadvantage: This vulnerability exists on a few devices and junk data is returned.
2. MAC ADDRESS
You can also obtain the mac address as the device id through Wifi.
Disadvantage: If Wifi is disabled, the hardware device may not return mac address ..
3. Serial Number
Android. OS. Build. SERIAL
Disadvantage: In a few devices, junk data is returned.
4. ANDROID_ID
ANDROID_ID is the number of 64bits generated and stored when the device is started for the first time,
Disadvantage: it is not applicable when the number of devices is changed after the device is set to wipe.
The android underlying layer is Linux. We can use the Linux method to obtain it:
1 cpu No:
File in:/proc/cpuinfo
Use the Adb shell to view the following information:
Adb shell cat/proc/cpuinfo
2 mac address
File Path:/sys/class/net/wlan0/address
Adb shell cat/sys/class/net/wlan0/address
Xx: aa
In this way, the serial numbers of the two can be obtained,
The method is determined, and the rest is the code.
Take the Mac address as an example:
String getMac (){
String macSerial = null;
String str = "";
Try {
Process pp = runtime.getruntime(cmd.exe c (
"Cat/sys/class/net/wlan0/address ");
InputStreamReader ir = new InputStreamReader (pp. getInputStream ());
LineNumberReader input = new LineNumberReader (ir );
For (; null! = Str ;){
Str = input. readLine ();
If (str! = Null ){
MacSerial = str. trim (); // remove space
Break;
}
}
} Catch (IOException ex ){
// Assign the default value
Ex. printStackTrace ();
}
Return macSerial;
}
Author: hpccn