Android programming gets the IMEI of the phone

Source: Internet
Author: User

Mobile phone in production, each phone has a unique identification (ID), International use of International Mobile device Identification Code (IMEI, International Mobile Equipment identity). IMEI is a 15-digit "E-string", which corresponds to one by one of each phone, and the code is the only one in the world. Each handset will be given a globally unique set of numbers that will be recorded by the manufacturer of the manufacturing process, from production to delivery.

In mobile application development, the use of IMEI to do identity authentication is a common technical means, in the Android SDK, class Android.telephony. Telephonymanager provides mobile device information related operation and management.

1. Increase access to device status in Androidmanifest.xml:
<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>

2, get to the Telephonymanager instance through the context device, call the Getdeviceid method to get the IMEI:

Import android.telephony.*;
......
Telephonymanager telephonymanager= (Telephonymanager) This.getsystemservice (Context.telephony_service);
String Imei=telephonymanager.getdeviceid ();

It is worth noting that when running in the emulator, the Getdeviceid method returns always 000000000000000.

In addition, the Telephonymanager class also provides methods for obtaining additional information about your phone, such as:

    • Getline1number (): Get the phone number;
    • Getdevicesoftwareversion: Gets the version of the Android operating system;
    • Getsimserialnumber: Gets the unique number ID of the SIM card;
    • Getsubscriberid: Obtain the customer ID, i.e. IMSI;
    • Use this piece of code when you get Mobile information from Android:

      public class Basicinfo {

      Public String Getphonenumber ()

      {

      Get phone number MSISDN, most likely empty

      Telephonymanager TM = (telephonymanager) getsystemservice(context.telephony_service);

      StringBuffer inf = new StringBuffer ();

      Switch (tm.getsimstate ()) {//getsimstate () Gets the status of the SIM in the following 6 states

      Case TelephonyManager.SIM_STATE_ABSENT:inf.append ("no card"); return inf.tostring ();

      Case TelephonyManager.SIM_STATE_UNKNOWN:inf.append ("Unknown state"); return inf.tostring ();

      Case TelephonyManager.SIM_STATE_NETWORK_LOCKED:inf.append ("Requires Networkpin to unlock"); return inf.tostring ();

      Case TelephonyManager.SIM_STATE_PIN_REQUIRED:inf.append ("Requires PIN to unlock"); return inf.tostring ();

      Case TelephonyManager.SIM_STATE_PUK_REQUIRED:inf.append ("Requires PUK to unlock"); return inf.tostring ();

      Case TelephonyManager.SIM_STATE_READY:break;

      }

      String PhoneNumber = Tm.getline1number ();

      return phonenumber;

      }

      ......

      }

      When called in another activity class, there is always a process shutdown to get the phone information.

      Later found

      getSystemService这个方法基于context,只有存在TextView控件的窗体中这个方法才会被激活~

      So:

      1.

      Add a constructor with the parameter context to Basicinfo:

      Public Basicinfo (Context context)

      {

      This.context = context;

      }

      2.

      The Getphonenumber () function is changed into:

      Context.getsystemservice (Context.telephony_servic);

      3.

      Inside the Calling class Basicinfo bi = new Basicinfo (this);

      Bi.getphonenumber ();

      Problem solving ...

Android programming gets the IMEI of the phone

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.