Android obtains mobile phone information

Source: Internet
Author: User

Android. some Classes in jar provide mobile phone-related information, such as Android. telephony. telephonymanager, android. OS. to view more information, go to frameworks to view more detailed class relationships. Copy a piece of code to briefly introduce some information about the mobile phone. The Code is as follows:

Import Java. util. locale; import android. app. activity; import android. content. context; import android. OS. build; import android. OS. bundle; import android. telephony. telephonymanager; import android. util. displaymetrics; import android. util. log; import android. widget. textview; public class phoneinfoactivity extends activity {/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); textview TV = (textview) findviewbyid (R. id. text); TV. settext (fetch_status (); getlocalmsg (); getphoneinfo ();} Public String fetch_status () {telephonymanager TM = (telephonymanager) This. getsystemservice (context. telephony_service); // string STR = ""; STR + = "DeviceID (IMEI) =" + TM. getdeviceid () + "\ n"; STR + = "devicesoftwareversion =" + TM. getdevicesoftwareversion () + "/N"; STR + = "line1number =" + TM. getline1number () + "\ n"; STR + = "networkcountryiso =" + TM. getnetworkcountryiso () + "\ n"; STR + = "networkoperator =" + TM. getnetworkoperator () + "\ n"; STR + = "networkoperatorname =" + TM. getnetworkoperatorname () + "\ n"; STR + = "networktype =" + TM. getnetworktype () + "\ n"; STR + = "honetype =" + TM. getphonetype () + "\ n"; STR + = "simcountryiso =" + TM. getsimcountryiso () + "\ n"; STR + = "simoperator =" + TM. getsimoperator () + "\ n"; STR + = "simoperatorname =" + TM. getsimoperatorname () + "\ n"; STR + = "simserialnumber =" + TM. getsimserialnumber () + "\ n"; STR + = "simstate =" + TM. getsimstate () + "\ n"; STR + = "subscriberid (imsi) =" + TM. getsubscriberid () + "\ n"; STR + = "voicemailnumber =" + TM. getvoicemailnumber () + "\ n"; return STR ;}// obtain information about the current mobile phone system public void getlocalmsg () {string STR = locale. getdefault (). getlanguage (); // obtain the current system language log of the mobile phone. V ("tag", "language" + Str); STR = locale. getdefault (). getcountry (); // obtain the country code log. V ("tag", "countrynumber" + Str);} // obtain the mobile phone information private void getphoneinfo () {stringbuffer sb = new stringbuffer (); string Language = getresources (). getconfiguration (). locale. getcountry (); sb. append (language ). append ("\ n"); displaymetrics dm = getresources (). getdisplaymetrics (); int W = DM. widthpixels; int H = DM. heightpixels; string screen_size = W + "*" + H; sb. append (screen_size ). append ("\ n"); string device = build. model; // mobile phone model sb. append (device ). append ("\ n"); string osver = android. OS. build. version. SDK; // mobile SDK version-level-4sb. append (osver ). append ("\ n"); string relea = android. OS. build. version. release; // mobile SDK version 1.6sb.append (relea ). append ("\ n"); telephonymanager = (telephonymanager) getsystemservice (context. telephony_service); string IMEI = telephonymanager. getdeviceid (); // imeisb of the mobile phone. append (IMEI ). append ("\ n"); string inclueno = telephonymanager. getline1number (); // mobile phone number sb. append (mobileno ). append ("\ n"); log. V ("tag", "countrynumber" + sb. tostring ());}}

The role of telephonymanager is also easy to understand. For details, see:

/** * Provides access to information about the telephony services on * the device. Applications can use the methods in this class to * determine telephony services and states, as well as to access some * types of subscriber information. Applications can also register * a listener to receive notification of telephony state changes. * <p> * You do not instantiate this class directly; instead, you retrieve * a reference to an instance through * {@link android.content.Context#getSystemService * Context.getSystemService(Context.TELEPHONY_SERVICE)}. * <p> * Note that access to some telephony information is * permission-protected. Your application cannot access the protected * information unless it has the appropriate permissions declared in * its manifest file. Where permissions apply, they are noted in the * the methods through which you access the protected information. */

This class provides some information and status for accessing the mobile phone service. For example, you can register a listener to obtain the phone status. This class is generally not directly instantiated, but obtained through context. getsystemservice (context. telephony_service.

Because the telephone service is a core function service of mobile phones, it requires specific permissions to obtain many services. Therefore, you must declare permissions to use the service.

In this example, you need to add permissions:

<Uses-Permission Android: Name = "android. Permission. read_phone_state"/>

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.