Android open-source code explanation: Using telephonymanager to obtain Mobile Network Information

Source: Internet
Author: User

This article describes how to use telephonymanager to obtain the SIM card status and mobile network information. It mainly uses the telephonymanager. Listen function. The source code of this function is as follows:

Public void listen (phonestatelistener listener, int events) {<br/> string pkgfordebug = mcontext! = NULL? Mcontext. getpackagename (): "<Unknown>"; <br/> try {<br/> Boolean policynow = (getitelephony ()! = NULL); <br/> mregistry. listen (pkgfordebug, listener. callback, events, policynow); <br/>} catch (RemoteException ex) {<br/> // system process dead <br/>} catch (nullpointerexception ex) {<br/> // system process dead <br/>}< br/>}

The specific implementation is not the focus of this Article. Here we only want to understand the two parameters of the function:

1) phonestatelistener listener

Generally, the corresponding callback function interface is implemented based on the events value and our processing is performed in the callback function. These interfaces include:

Public void onservicestatechanged (servicestate) <br/> Public void onmessagewaitingindicatorchanged (Boolean MWI) <br/> Public void reset (Boolean CFI) <br/> Public void oncelllocationchanged (celllocation location) <br/> Public void oncallstatechanged (INT state, string incomingnumber) <br/> Public void ondataconnectionstatechanged (INT state, int networktype) <br/> Public void ondataactivity (INT direction) <br/> Public void onsignalstrengthschanged (signalstrength)

2) int events

The events value is as follows:

Public static final int listen_none = 0; // stop listening <br/> Public static final int listen_service_state = 0x00000001; <br/> Public static final int listen_message_waiting_indicator = 0x00000004; <br/> Public static final int listen_call_forwarding_indicator = 0x00000008; <br/> Public static final int listen_cell_location = 0x00000010; <br/> Public static final int listen_call_state = 0x00000020; <br/> Public static final int listen_data_connection_state = 0x00000040; <br/> Public static final int listen_data_activity = 0x00000080; <br/> Public static final int listen_signal_strengths = 0x00000100;

The code for using the above knowledge points is as follows. First look at the layout file network_detector.xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview Android: id = "@ + ID/phone_type" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"/> <br/> <textview Android: Id = "@ + ID/network_name" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"/> <br/> <textview Android: id = "@ + ID/sim_state" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"/> <br/> <textview Android: Id = "@ + ID/network_type" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"/> <br/> </linearlayout>

The complete Java code is as follows (the parsing of apidemos has been deleted and modified ):

Package HUST. iprai. asce1885; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. telephony. phonestatelistener; <br/> Import android. telephony. servicestate; <br/> Import android. telephony. telephonymanager; <br/> Import android. widget. textview; </P> <p> public class networkdetector extends activity {</P> <p> // SIM card status constant <br/> Private Static final string sim_absent = "absent "; // No SIM card in the mobile phone <br/> Private Static final string sim_ready = "ready"; // the SIM card is ready <br/> Private Static final string sim_pin_required = "pin required "; // unlock the SIM card pin <br/> Private Static final string sim_puk_required = "Puk required "; // unlock the PUK that requires the SIM card <br/> Private Static final string sim_network_locked = "Network locked "; // network pin unlock required <br/> Private Static final string sim_unknown = "unknown"; // Unknown Status </P> <P> // network type constant <br/> Private Static final string network_cdma = "CDMA: either is95a or is95b (2g )"; <br/> Private Static final string network_edge = "edge (2.75g)"; <br/> Private Static final string network_gprs = "GPRS (2.5g )"; <br/> Private Static final string network_umts = "UMTS (3G)"; <br/> Private Static final string network_evdo_0 = "evdo Revision 0 (3G )"; <br/> Private Static final string networ K_evdo_a = "evdo Revision A (3g-Transitional)"; <br/> Private Static final string network_evdo_ B = "evdo Revision B (3g-Transitional )"; <br/> Private Static final string network_1x_rtt = "1 xrtt (2g-Transitional)"; <br/> Private Static final string network_hsdpa = "HSDPA (3g-Transitional) "; <br/> Private Static final string network_hsupa =" hsupa (3g-Transitional) "; <br/> Private Static F Inal string network_hspa = "HSPA (3g-Transitional)"; <br/> Private Static final string network_iden = "iden (2g )"; <br/> Private Static final string network_unkown = "unknown "; </P> <p> // Mobile Phone standard type constant <br/> Private Static final string phone_cdma = "CDMA "; <br/> Private Static final string phone_gsm = "GSM"; <br/> Private Static final string phone_none = "no radio "; </P> <p>/** called when the activity Is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. network_detector); </P> <p> // obtains the telephony system service for obtaining SIM card and network-related information. <br/> final telephonymanager TM = (telephonymanager) getsystemservice (telephony_service ); </P> <p> // Update network information on the Interface <br/> updateviews (TM ); </P> <p> // because the SIM card status and network information may change at any time, you need to register Pho Nestatelistener <br/> // to update the information displayed on the interface in real time. Two events are monitored here: listen_service_state and listen_data_connection_state <br/> // they correspond to the callback functions onservicestatechanged and ondataconnectionstatechanged respectively <br/> TM. listen (New phonestatelistener () {</P> <p> @ override <br/> Public void ondataconnectionstatechanged (INT state, int networktype) {<br/> // a change in the data connection status may lead to a change in the network type <br/> updateviews (TM ); <br/>}</P> <p> @ override <br/> Public void onservicestatechanged (servicestate) {<br/> updateviews (TM ); <br/>}</P> <p >}, phonestatelistener. listen_service_state <br/> | phonestatelistener. listen_data_connection_state); <br/>}</P> <p> @ override <br/> protected void onresume () {<br/> super. onresume (); <br/> final telephonymanager TM = (telephonymanager) getsystemservice (telephony_service); <br/> updateviews (TM ); <br/>}</P> <p>/** <br/> * update SIM card status and network information <br/> * @ Param tm <br/> */ <br/> private final void updateviews (telephonymanager TM) {<br/> textview view = NULL; </P> <p> View = (textview) findviewbyid (R. id. sim_state); <br/> View. settext ("Sim state:" + mapsimstatetoname (TM. getsimstate (); </P> <p> View = (textview) findviewbyid (R. id. network_type); <br/> View. settext ("network type:" + mapnetworktypetoname (TM. getnetworktype (); </P> <p> View = (textview) findviewbyid (R. id. network_name); <br/> View. settext ("network operator:" + TM. getnetworkoperatorname (); </P> <p> View = (textview) findviewbyid (R. id. phone_type); <br/> View. settext ("phone type:" + mapdevicetypetoname (TM. getphonetype ())); </P> <p >}</P> <p>/** <br/> * return the mobile phone standard value in string format <br/> * @ Param phonetype <br/> * @ return <br/> */<br/> private string mapdevicetypetoname (INT phonetype) {<br/> switch (phonetype) {<br/> case telephonymanager. phone_type_cdma: <br/> return phone_cdma; <br/> case telephonymanager. phone_type_gsm: <br/> return phone_gsm; <br/> case telephonymanager. phone_type_none: <br/> return phone_none; <br/> default: <br/> // This branch is not allowed. <br/> return NULL; <br/>}</P> <p>/** <br/> * returns the network type value as a string. <br/> * @ Param networktype <br/> * @ return <br/> */<br/> private string mapnetworktypetoname (INT networktype) {<br/> switch (networktype) {<br/> case telephonymanager. network_type_cdma: <br/> return network_cdma; <br/> case telephonymanager. network_type_edge: <br/> return network_edge; <br/> case telephonymanager. network_type_gprs: <br/> return network_gprs; <br/> case telephonymanager. network_type_umts: <br/> return network_umts; <br/> case telephonymanager. network_type_evdo_0: <br/> return network_evdo_0; <br/> case telephonymanager. network_type_evdo_a: <br/> return network_evdo_a; <br/> case telephonymanager. network_type_evdo_ B: <br/> return network_evdo_ B; <br/> case telephonymanager. network_type_1xrtt: <br/> return network_1x_rtt; <br/> case telephonymanager. network_type_hsdpa: <br/> return network_hsdpa; <br/> case telephonymanager. network_type_hspa: <br/> return network_hspa; <br/> case telephonymanager. network_type_hsupa: <br/> return network_hsupa; <br/> case telephonymanager. network_type_iden: <br/> return network_iden; <br/> case telephonymanager. network_type_unknown: <br/> default: <br/> return network_unkown; <br/>}</P> <p>/** <br/> * returns the SIM card status value as a string. <br/> * @ Param simstate <br/> * @ return <br/> */<br/> Private Static string mapsimstatetoname (INT simstate) {<br/> switch (simstate) {<br/> case telephonymanager. sim_state_absent: <br/> return sim_absent; <br/> case telephonymanager. sim_state_ready: <br/> return sim_ready; <br/> case telephonymanager. sim_state_pin_required: <br/> return sim_pin_required; <br/> case telephonymanager. sim_state_puk_required: <br/> return sim_puk_required; <br/> case telephonymanager. sim_state_network_locked: <br/> return sim_network_locked; <br/> case telephonymanager. sim_state_unknown: <br/> return sim_unknown; <br/> default: <br/> // This branch is not allowed. <br/> return NULL; <br/>}</P> <p>}

Program Execution result:

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.