<span id="Label3"></p>Background<p><p>Some time ago, a power company was given a management system to investigate the Company's customers ' willingness to purchase a training course for electric power companies and to provide the ability to order Orders.</p></p><p><p><em id="__mceDel"> </em>Because the power company asked to run on the Android tablet, it was developed for the 7-inch Android tablet, and I had to test it on my Android phone until the night before the project was submitted, without proper device debugging.<br>Customer feedback said the system stopped running after the submission of the order, import data to the PC server after the service end also crashed!<br>I started to suspect that because of their poor equipment-i was in the configuration of quite low Android native virtual machines (how low you should be aware of the-_-) on the debugging without a crash problem, why on a state-owned Flat-panel device will not run?<br>Helpless to let customers send their equipment to the actual machine debugging, the results found unexpectedly in a log reported an empty exception:</p></p><pre><pre><span style="color: #008080;">1</span> Telephonymanager tm =<span style="color: #000000;"> (telephonymanager) getsystemservice (context.telephony_service);</span> <span style="color: #008080;">2</span> String device_id =<span style="color: #000000;"> Tm.getdeviceid ();</span> <span style="color: #008080;">3</span> <span style="color: #008000;">//</span> <span style="color: #008000;">Exception</span></pre></pre><p><p>originally, Android was the first mobile device, and now there are non-mobile devices: such as tablets, ebooks, televisions, music players and so On. These devices do not have the hardware function of the call, there is no telephony_service in the system, and naturally it is impossible to obtain device_id by the above Method. The value of all device_id is null, which causes null values in the database, and the server and client share the database, so the server is also Hung. So it all makes Sense.</p></p>Overview<p><p>Sometimes the user device needs to be identified, so you want to get a stable and reliable and unique identification Code. Although such device identifiers are available in the Android system, stability and uniqueness are not ideal due to limitations such as the Android version and bugs in the Vendor-specific system. And through other hardware information identification also because of the system version, mobile phone hardware and other limitations there are different degrees of problems.<br>The following collection of "capable" or "capable" as a device identification of the serial Code.</p></p>device_id<p><p>This is an Android system for developers to identify the serial number of mobile devices, but also a variety of methods of high universality, it can be said that almost all devices can return this serial number, and the uniqueness of Good.</p></p><p><p>This device_id can be obtained with the following methods:</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;"></pre></pre><p><p>It will return the Imei,meid or ESN code depending on the phone device, but the following problems are used in the Process:</p></p> <ul> <ul> <li>Non-mobile devices: Android devices are the first to be installed, and now there are non-mobile devices: such as tablets, ebooks, tvs, music players and so On. These devices do not have the hardware function of the call, there is no telephony_service in the system, and naturally it is impossible to obtain device_id by the above Method.</li> <li>Permission problem: get device_id need read_phone_state permission, if just to get device_id and no other call function, request this permission to a large only small use, two deploy users will suspect the security of the Software.</li> <li>Bug in Vendor-specific system: a few mobile devices, because of the implementation of the vulnerability, will return garbage, such as: zeros or asterisks</li> </ul> </ul>MAC ADDRESS<p><p>You can use your Phone's WiFi or Bluetooth MAC address as your device id, but this is not recommended for the following two reasons:</p></p> <ul> <ul> <li>Hardware limitations: Not all devices have WiFi and Bluetooth hardware, the hardware does not exist naturally will not get this Information.</li> <li>Gets the limit: if WiFi is not turned on, it cannot get its Mac address, while Bluetooth is only available when it is opened to its Mac Address.</li> </ul> </ul><p><p>Get WiFi mac Address:</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;">Permissions: <uses-permission android:name= "android.permission.ACCESS_WIFI_STATE" ></uses-permission> Wifimanager WiFi = (wifimanager) getsystemservice (context.wifi_service); Wifiinfo info = wifi.getconnectioninfo (); String Wifi_mac = info.getmacaddress ();</pre></pre>Sim Serial Number<p><p>Device with SIM card, you can get to sim Serial number by the following method:</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;"></pre></pre><p><p>Note: for CDMA devices, including those with no SIM card on the tablet, a null value is returned!</p></p>android_id<p><p>When the device first starts, the system randomly generates a 64-bit number and saves the number in the form of a 16 string, which is a 16-android_id string that is reset when the device is Wipe. The following methods can be obtained:</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;">Import android.provider.Settings; </pre></pre><p><p>The android_id can be used as a device identification, but it is important to note:</p></p> <ul> <ul> <li>vendor-customized system bug: different devices may produce the same android_id:9774d56d682e549c.</li> <li>vendor-customized system Bug: Some devices return a value of Null.</li> <li>Device differences: for CDMA devices, android_id and Telephonymanager.getdeviceid () return the same values.</li> </ul> </ul>Serial number<p><p>Android System version 2.3 and above can be obtained by the following method serial number, and Non-mobile devices can also be obtained through the INTERFACE.</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;"></pre></pre>Installtion ID<p><p>There are some limitations or bugs in some of these ways, and if you do not really need to bind the hardware itself, using your own generated UUID is a good choice, because it does not require access to the Device's resources, and is not related to the device type.</p></p><p><p>The principle of this approach is to generate an ID when the program is installed the first time it is run, which differs from the Device's unique identity, and different applications produce different ids, and the same program can be reinstalled Differently. So this is not the Device's unique id, but it guarantees that each User's ID will be Different. It can be said to be used to identify the unique ID of each application (that is, the installtion id), which can be used to track the number of apps Installed.</p></p><p><p>Google Developer Blog provides such a framework:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.io.File;</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.io.FileOutputStream;</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.io.IOException;</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.io.RandomAccessFile;</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.util.UUID;</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.content.Context;</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #008000;"><span style="color: #008000;">* Google Developer Blog, which provides a framework for obtaining a unique identifier for a device</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #008000;"><span style="color: #008000;"> * </span></span><span style="color: #808080;"><span style="color: #808080;">@author</span></span><span style="color: #008000;"><span style="color: #008000;">NULL1943</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #008000;"><span style="color: #008000;"> *</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">testinstallation {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span>String SID =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>String installation = "installation"<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">synchronized</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span><span style="color: #000000;"><span style="color: #000000;">String ID (context Context) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(sID = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>File installation =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">File (context.getfilesdir (), installation);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">installation.exists ())</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;">writeinstallationfile (installation);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> at</span>SID =<span style="color: #000000;"><span style="color: #000000;">readinstallationfile (installation);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}<span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(Exception E) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Throw</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">runtimeexception (e);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">sID;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> to</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span><span style="color: #000000;"><span style="color: #000000;">String readinstallationfile (File Installation)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;">throws</span></span><span style="color: #000000;"><span style="color: #000000;">IOException {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Randomaccessfile f =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Randomaccessfile (installation, "r"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">byte</span></span>[] bytes =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">byte</span></span>[(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">) f.length ()];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> *</span> <span style="color: #000000;"><span style="color: #000000;">f.readfully (bytes);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #000000;"><span style="color: #000000;">f.close ();</span></span><span style="color: #008080;"><span style="color: #008080;">Panax Notoginseng</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">String (bytes);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">writeinstallationfile (File Installation)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #0000ff;"><span style="color: #0000ff;">throws</span></span><span style="color: #000000;"><span style="color: #000000;">IOException {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>FileOutputStream out =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">FileOutputStream (installation);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>String ID =<span style="color: #000000;"><span style="color: #000000;">uuid.randomuuid (). toString ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">out.write (id.getbytes ());</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #000000;"><span style="color: #000000;">out.close ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</pre><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">Installation ID Get frame</span></span>Test results Demo:<p><p>Android Phone:</p></p><p><p></p></p><p><p>Android Tablet:</p></p><p><p></p></p><pre><pre><br>Reference: get the Android device unique ID code</pre></pre><p><p>20150919_ get Android Unique identification code</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service