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