Recently in the process of doing the project need to uniquely identify the user's device, background in the push notification need to use this unique identification number.
The first thing I'll think about is the device ID of the appliance, which can definitely identify the device, and the first version is exactly the same. National Day during the user's email let elder brother very not calm, because need to get device ID, so it is necessary to add permissions in the Androidmanifest file
<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>
Adding this permission is natural when the user downloads the app and prompts the following list of permissions to accept:
Ok. The question comes, the user thinks this thing is more sensitive, I choose not to install your This app, the heart 10,000 alpaca is running.
Think of another way to achieve this demand. Today and everyone summarizes the pros and cons of each of the ways to share.
The first way: Device ID for devices as a unique identifier
Adding permissions in the Androidmanifest configuration file
<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>
Here's how to get it: I wrote it in the tool class.
Public Static string Getdeviceidinfo (Context mcontext) { //string imei = ((Telephonymanager) Mcontext.getsystemservice (Mcontext.telephony_service)). Getdeviceid (); String IMEI = secure.getstring (Mcontext.getcontentresolver (), secure.android_id); return imei; }
The disadvantage of this approach is that:
1. Users who encounter high security alertness, I do not accept this permission, do not install your app.
2. Non-mobile device, if only the device with WiFi or music player does not have the hardware function of the call, there is no such device_id.
3. As a mobile phone, the IMEI is unique, it should be similar to 359881030314356 (unless you have a non-production cell phone (parallel) It may have invalid IMEI, such as: 0000000000000). Plainly, if only in order to get it, no other call function, then this permission is a bit large to use.
4. On a few mobile devices, the implementation is flawed and will return garbage, such as zeros or asterisks products.
Second way: Get Mac ADDRESS
We can also get the MAC address as the device ID from the phone's WiFi or Bluetooth device, but it is not recommended because not all devices have WiFi and if WiFi is not turned on, the hardware device cannot return to MAC address.
The WLAN MAC Address string, which is another unique ID. There is no doubt that you need to add the following permissions to the Androidmanifest file:
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/>
Implementation in the code:
Wifimanager wm = (Wifimanager) getsystemservice (Context.wifi_service);
String M_szwlanmac = Wm.getconnectioninfo (). getmacaddress ();
Returns:00:11:22:33:44:55 (This is not a real address.) And this address can easily be forged. ). The WLAN does not have to be opened to read some values.
The Third Way: BT MAC ADDRESS
Run only on devices that have Bluetooth. And to join Android.permission.BLUETOOTH permissions.
NULL // Local Bluetooth Adapter
M_bluetoothadapter = Bluetoothadapter.getdefaultadapter ();
String M_szbtmac = m_bluetoothadapter.getaddress ();
returns:43:25:78:50:93:38. Bluetooth is not necessary to open, also can read.
Fourth Way: Serial number
Devices equipped with a SIM card can be Getsystemservice (Context.telephony_serviec). Getsimserialnumber (); Get to
Sim Serial number. Note For CDMA devices, a null value is returned.
The Android 2.3 can be obtained via Android.os.Build.SERIAL, and non-mobile devices can be obtained through this interface.
Fifth way: android_id
ANDROID_ID is a number of 64bit that is generated and stored when the device is first started and resets when the device is wipe
ANDROID_ID seems to be a good choice to get a device ID, but it's also flawed:
It is reliable and stable in Android <=2.1 or Android >=2.3 version, but the version in 2.2 is not 100% reliable
In the mainstream manufacturers of equipment, there is a very frequent bug, that every device will produce the same android_id:9774d56d682e549c
Specific methods of obtaining:
String M_szandroidid = secure.getstring (Getcontentresolver (), secure.android_id);
Sixth way: Installtion Id:uuid
This is the way I finally adopted it.
There are some limitations or bugs in some of the above methods, here, there is another way to solve, is to use the UUID, the method does not need to access the resources of the device, is not related to the device type.
This is done by generating an ID after the first run of the program installation, but unlike the device's unique identity, it produces different IDs for different applications than the device unique IDs.
This is often used to identify a unique ID in an app (that is, the Installtion ID), or to track the number of installs of an app.
I've created a new tool class in my program to get this UUID.
ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.RandomAccessFile;ImportJava.util.UUID;ImportAndroid.content.Context; Public classInstallation {Private StaticString SID =NULL; Private Static FinalString installation = "Installation"; Public synchronized StaticString ID (context context) {if(SID = =NULL) {File installation=NewFile (Context.getfilesdir (), installation); Try { if(!installation.exists ()) writeinstallationfile (installation); SID=readinstallationfile (installation); } Catch(Exception e) {Throw NewRuntimeException (e); } } returnSID; } Private StaticString readinstallationfile (File installation)throwsIOException {randomaccessfile f=NewRandomaccessfile (Installation, "R"); byte[] bytes =New byte[(int) F.length ()]; f.readfully (bytes); F.close (); return NewString (bytes); } Private Static voidWriteinstallationfile (File installation)throwsIOException {FileOutputStream out=NewFileOutputStream (installation); String ID=Uuid.randomuuid (). toString (); Out.write (Id.getbytes ()); Out.close (); }}
To sum up, I would rather recommend the last way. If there are other ways to implement you can leave a message to study the discussion.
Get a physical unique ID on your Android phone