Android——TelephonyManager类使用方法大全

转自http://blog.163.com/liu_jun_y/blog/static/188086312201171572833546/  


这个类是很有用地,可以得到很多关于手机的信息,做应用时必须的工具。 
不废话,直接上注释后的代码,请享用: 
Java代码   收藏代码
  1. /** 
  2. * 
  3. *@author dingran 
  4. *创建日期 2010-4-29 下午05:02:47 
  5. * 
  6. */  
  7. package net.sunniwell.app;  
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.telephony.CellLocation;  
  11. import android.telephony.PhoneStateListener;  
  12. import android.telephony.TelephonyManager;  
  13. public class TelManager extends Activity {  
  14. @Override  
  15. protected void onCreate(Bundle savedInstanceState) {  
  16.   super.onCreate(savedInstanceState);  
  17.   TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);  
  18.     
  19.   /* 
  20.    * 电话状态: 
  21.    * 1.tm.CALL_STATE_IDLE=0          无活动 
  22.    * 2.tm.CALL_STATE_RINGING=1  响铃 
  23.    * 3.tm.CALL_STATE_OFFHOOK=2  摘机 
  24.    */  
  25.   tm.getCallState();//int  
  26.     
  27.   /* 
  28.    * 电话方位: 
  29.    *  
  30.    */  
  31.   tm.getCellLocation();//CellLocation  
  32.     
  33.   /* 
  34.    * 唯一的设备ID: 
  35.    * GSM手机的 IMEI 和 CDMA手机的 MEID.  
  36.    * Return null if device ID is not available. 
  37.    */  
  38.   tm.getDeviceId();//String  
  39.     
  40.   /* 
  41.    * 设备的软件版本号: 
  42.    * 例如:the IMEI/SV(software version) for GSM phones. 
  43.    * Return null if the software version is not available.  
  44.    */  
  45.   tm.getDeviceSoftwareVersion();//String  
  46.     
  47.   /* 
  48.    * 手机号: 
  49.    * GSM手机的 MSISDN. 
  50.    * Return null if it is unavailable.  
  51.    */  
  52.   tm.getLine1Number();//String  
  53.     
  54.   /* 
  55.    * 附近的电话的信息: 
  56.    * 类型:List<NeighboringCellInfo>  
  57.    * 需要权限:android.Manifest.permission#ACCESS_COARSE_UPDATES 
  58.    */  
  59.   tm.getNeighboringCellInfo();//List<NeighboringCellInfo>  
  60.     
  61.   /* 
  62.    * 获取ISO标准的国家码,即国际长途区号。 
  63.    * 注意:仅当用户已在网络注册后有效。 
  64.    *       在CDMA网络中结果也许不可靠。 
  65.    */  
  66.   tm.getNetworkCountryIso();//String  
  67.     
  68.   /* 
  69.    * MCC+MNC(mobile country code + mobile network code) 
  70.    * 注意:仅当用户已在网络注册时有效。 
  71.    *    在CDMA网络中结果也许不可靠。 
  72.    */  
  73.   tm.getNetworkOperator();//String  
  74.     
  75.   /* 
  76.    * 按照字母次序的current registered operator(当前已注册的用户)的名字 
  77.    * 注意:仅当用户已在网络注册时有效。 
  78.    *    在CDMA网络中结果也许不可靠。 
  79.    */  
  80.   tm.getNetworkOperatorName();//String  
  81.     
  82.   /* 
  83.    * 当前使用的网络类型: 
  84.    * 例如: NETWORK_TYPE_UNKNOWN  网络类型未知  0 
  85.      NETWORK_TYPE_GPRS     GPRS网络  1 
  86.      NETWORK_TYPE_EDGE     EDGE网络  2 
  87.      NETWORK_TYPE_UMTS     UMTS网络  3 
  88.      NETWORK_TYPE_HSDPA    HSDPA网络  8  
  89.      NETWORK_TYPE_HSUPA    HSUPA网络  9 
  90.      NETWORK_TYPE_HSPA     HSPA网络  10 
  91.      NETWORK_TYPE_CDMA     CDMA网络,IS95A 或 IS95B.  4 
  92.      NETWORK_TYPE_EVDO_0   EVDO网络, revision 0.  5 
  93.      NETWORK_TYPE_EVDO_A   EVDO网络, revision A.  6 
  94.      NETWORK_TYPE_1xRTT    1xRTT网络  7 
  95.    */  
  96.   tm.getNetworkType();//int  
  97.     
  98.   /* 
  99.    * 手机类型: 
  100.    * 例如: PHONE_TYPE_NONE  无信号 
  101.      PHONE_TYPE_GSM   GSM信号 
  102.      PHONE_TYPE_CDMA  CDMA信号 
  103.    */  
  104.   tm.getPhoneType();//int  
  105.     
  106.   /* 
  107.    * Returns the ISO country code equivalent for the SIM provider's country code. 
  108.    * 获取ISO国家码,相当于提供SIM卡的国家码。 
  109.    *  
  110.    */  
  111.   tm.getSimCountryIso();//String  
  112.     
  113.   /* 
  114.    * Returns the MCC+MNC (mobile country code + mobile network code) of the provider of the SIM. 5 or 6 decimal digits. 
  115.    * 获取SIM卡提供的移动国家码和移动网络码.5或6位的十进制数字. 
  116.    * SIM卡的状态必须是 SIM_STATE_READY(使用getSimState()判断). 
  117.    */  
  118.   tm.getSimOperator();//String  
  119.     
  120.   /* 
  121.    * 服务商名称: 
  122.    * 例如:中国移动、联通 
  123.    * SIM卡的状态必须是 SIM_STATE_READY(使用getSimState()判断). 
  124.    */  
  125.   tm.getSimOperatorName();//String  
  126.     
  127.   /* 
  128.    * SIM卡的序列号: 
  129.    * 需要权限:READ_PHONE_STATE 
  130.    */  
  131.   tm.getSimSerialNumber();//String  
  132.     
  133.   /* 
  134.    * SIM的状态信息: 
  135.    *  SIM_STATE_UNKNOWN          未知状态 0 
  136.    SIM_STATE_ABSENT           没插卡 1 
  137.    SIM_STATE_PIN_REQUIRED     锁定状态,需要用户的PIN码解锁 2 
  138.    SIM_STATE_PUK_REQUIRED     锁定状态,需要用户的PUK码解锁 3 
  139.    SIM_STATE_NETWORK_LOCKED   锁定状态,需要网络的PIN码解锁 4 
  140.    SIM_STATE_READY            就绪状态 5 
  141.    */  
  142.   tm.getSimState();//int  
  143.     
  144.   /* 
  145.    * 唯一的用户ID: 
  146.    * 例如:IMSI(国际移动用户识别码) for a GSM phone. 
  147.    * 需要权限:READ_PHONE_STATE 
  148.    */  
  149.   tm.getSubscriberId();//String  
  150.     
  151.   /* 
  152.    * 取得和语音邮件相关的标签,即为识别符 
  153.    * 需要权限:READ_PHONE_STATE 
  154.    */  
  155.   tm.getVoiceMailAlphaTag();//String  
  156.     
  157.   /* 
  158.    * 获取语音邮件号码: 
  159.    * 需要权限:READ_PHONE_STATE 
  160.    */  
  161.   tm.getVoiceMailNumber();//String  
  162.     
  163.   /* 
  164.    * ICC卡是否存在 
  165.    */  
  166.   tm.hasIccCard();//boolean  
  167.     
  168.   /* 
  169.    * 是否漫游: 
  170.    * (在GSM用途下) 
  171.    */  
  172.   tm.isNetworkRoaming();//  
  173.     
  174.     
  175.     
  176. }  
  177. }  



源码 TelephonyManager.java: 
Java代码   收藏代码
  1. package android.telephony;  
  2. import com.android.internal.telephony.*;  
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import android.annotation.SdkConstant.SdkConstantType;  
  6. import android.annotation.SdkConstant;  
  7. import android.content.Context;  
  8. import android.os.Bundle;  
  9. import android.os.RemoteException;  
  10. import android.os.ServiceManager;  
  11. import android.os.SystemProperties;  
  12. /** 
  13. * Provides access to information about the telephony services on 
  14. * the device. Applications can use the methods in this class to 
  15. * determine telephony services and states, as well as to access some 
  16. * types of subscriber information. Applications can also register  
  17. * a listener to receive notification of telephony state changes.  
  18. * <p> 
  19. * You do not instantiate this class directly; instead, you retrieve 
  20. * a reference to an instance through  
  21. * {@link android.content.Context#getSystemService 
  22. * Context.getSystemService(Context.TELEPHONY_SERVICE)}. 
  23. * <p> 
  24. * Note that acess to some telephony information is 
  25. * permission-protected. Your application cannot access the protected  
  26. * information unless it has the appropriate permissions declared in  
  27. * its manifest file. Where permissions apply, they are noted in the   
  28. * the methods through which you access the protected information.  
  29. */  
  30. public class TelephonyManager {  
  31.     private static final String TAG = "TelephonyManager";  
  32.     private Context mContext;  
  33.     private ITelephonyRegistry mRegistry;  
  34.     /** @hide */  
  35.     public TelephonyManager(Context context) {  
  36.         mContext = context;  
  37.         mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(  
  38.                     "telephony.registry"));  
  39.     }  
  40.     /** @hide */  
  41.     private TelephonyManager() {  
  42.     }  
  43.     private static TelephonyManager sInstance = new TelephonyManager();  
  44.     /** @hide */  
  45.     public static TelephonyManager getDefault() {  
  46.         return sInstance;  
  47.     }  
  48.     //  
  49.     // Broadcast Intent actions  
  50.     //  
  51.     /** 
  52.      * Broadcast intent action indicating that the call state (cellular) 
  53.      * on the device has changed. 
  54.      * 
  55.      * <p> 
  56.      * The {@link #EXTRA_STATE} extra indicates the new call state. 
  57.      * If the new state is RINGING, a second extra 
  58.      * {@link #EXTRA_INCOMING_NUMBER} provides the incoming phone number as 
  59.      * a String. 
  60.      * 
  61.      * <p class="note"> 
  62.      * Requires the READ_PHONE_STATE permission. 
  63.      * 
  64.      * <p class="note"> 
  65.      * This was a {@link android.content.Context#sendStickyBroadcast sticky} 
  66.      * broadcast in version 1.0, but it is no longer sticky. 
  67.      * Instead, use {@link #getCallState} to synchronously query the current call state. 
  68.      * 
  69.      * @see #EXTRA_STATE 
  70.      * @see #EXTRA_INCOMING_NUMBER 
  71.      * @see #getCallState 
  72.      */  
  73.     @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)  
  74.     public static final String ACTION_PHONE_STATE_CHANGED =  
  75.             "android.intent.action.PHONE_STATE";  
  76.     /** 
  77.      * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast 
  78.      * for a String containing the new call state. 
  79.      * 
  80.      * @see #EXTRA_STATE_IDLE 
  81.      * @see #EXTRA_STATE_RINGING 
  82.      * @see #EXTRA_STATE_OFFHOOK 
  83.      * 
  84.      * <p class="note"> 
  85.      * Retrieve with 
  86.      * {@link android.content.Intent#getStringExtra(String)}. 
  87.      */  
  88.     public static final String EXTRA_STATE = Phone.STATE_KEY;  
  89.     /** 
  90.      * Value used with {@link #EXTRA_STATE} corresponding to 
  91.      * {@link #CALL_STATE_IDLE}. 
  92.      */  
  93.     public static final String EXTRA_STATE_IDLE = Phone.State.IDLE.toString();  
  94.     /** 
  95.      * Value used with {@link #EXTRA_STATE} corresponding to 
  96.      * {@link #CALL_STATE_RINGING}. 
  97.      */  
  98.     public static final String EXTRA_STATE_RINGING = Phone.State.RINGING.toString();  
  99.     /** 
  100.      * Value used with {@link #EXTRA_STATE} corresponding to 
  101.      * {@link #CALL_STATE_OFFHOOK}. 
  102.      */  
  103.     public static final String EXTRA_STATE_OFFHOOK = Phone.State.OFFHOOK.toString();  
  104.     /** 
  105.      * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast 
  106.      * for a String containing the incoming phone number. 
  107.      * Only valid when the new call state is RINGING. 
  108.      * 
  109.      * <p class="note"> 
  110.      * Retrieve with 
  111.      * {@link android.content.Intent#getStringExtra(String)}. 
  112.      */  
  113.     public static final String EXTRA_INCOMING_NUMBER = "incoming_number";  
  114.     //  
  115.     //  
  116.     // Device Info  
  117.     //  
  118.     //  
  119.     /** 
  120.      * Returns the software version number for the device, for example,  
  121.      * the IMEI/SV for GSM phones. 
  122.      * 
  123.      * <p>Requires Permission:  
  124.      *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 
  125.      */  
  126.     public String getDeviceSoftwareVersion() {  
  127.         try {  
  128.             return getSubscriberInfo().getDeviceSvn();  
  129.         } catch (RemoteException ex) {  
  130.         }  
  131.         return null;  
  132.     }  
  133.     /** 
  134.      * Returns the unique device ID, for example,the IMEI for GSM 
  135.      * phones. 
  136.      * 
  137.      * <p>Requires Permission:  
  138.      *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 
  139.      */  
  140.     public String getDeviceId() {  
  141.         try {  
  142.             return getSubscriberInfo().getDeviceId();  
  143.         } catch (RemoteException ex) {  
  144.         }  
  145.         return null;  
  146.     }  
  147.     /** 
  148.      * Returns the current location of the device. 
  149.      * 
  150.      * <p>Requires Permission: {@link android.Manifest.permission#ACCESS_COARSE_LOCATION 
  151.      * ACCESS_COARSE_LOCATION}. 
  152.      */  
  153.     public CellLocation getCellLocation() {  
  154.         try {  
  155.             Bundle bundle = getITelephony().getCellLocation();  
  156.             return CellLocation.newFromBundle(bundle);  
  157.         } catch (RemoteException ex) {  
  158.         }  
  159.         return null;  
  160.     }  
  161.     /** 
  162.      * Enables location update notifications.  {@link PhoneStateListener#onCellLocationChanged 
  163.      * PhoneStateListener.onCellLocationChanged} will be called on location updates. 
  164.      * 
  165.      * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES  
  166.      * CONTROL_LOCATION_UPDATES} 
  167.      * 
  168.      * @hide 
  169.      */  
  170.     public void enableLocationUpdates() {  
  171.         try {  
  172.             getITelephony().enableLocationUpdates();  
  173.         } catch (RemoteException ex) {  
  174.         }  
  175.     }  
  176.     /** 
  177.      * Disables location update notifications.  {@link PhoneStateListener#onCellLocationChanged 
  178.      * PhoneStateListener.onCellLocationChanged} will be called on location updates. 
  179.      * 
  180.      * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES 
  181.      * CONTROL_LOCATION_UPDATES} 
  182.      * 
  183.      * @hide 
  184.      */  
  185.     public void disableLocationUpdates() {  
  186.         try {  
  187.             getITelephony().disableLocationUpdates();  
  188.         } catch (RemoteException ex) {  
  189.         }  
  190.     }  
  191.     /** 
  192.      * Returns the neighboring cell information of the device. 
  193.      *  
  194.      * @return List of NeighboringCellInfo or null if info unavailable. 
  195.      *  
  196.      * <p>Requires Permission:  
  197.      * (@link android.Manifest.permission#ACCESS_COARSE_UPDATES} 
  198.      */  
  199.     public List<NeighboringCellInfo> getNeighboringCellInfo() {  
  200.        try {  
  201.            return getITelephony().getNeighboringCellInfo();  
  202.        } catch (RemoteException ex) {  
  203.        }  
  204.        return null;  
  205.          
  206.     }  
  207.       
  208.     /** 
  209.      * No phone module 
  210.      */  
  211.     public static final int PHONE_TYPE_NONE = 0;  
  212.     /** 
  213.      * GSM phone 
  214.      */  
  215.     public static final int PHONE_TYPE_GSM = 1;  
  216.     /** 
  217.      * Returns a constant indicating the device phone type.  
  218.      *  
  219.      * @see #PHONE_TYPE_NONE 
  220.      * @see #PHONE_TYPE_GSM 
  221.      */  
  222.     public int getPhoneType() {  
  223.         // in the future, we should really check this  
  224.         return PHONE_TYPE_GSM;  
  225.     }  
  226.     //  
  227.     //   
  228.     // Current Network  
  229.     //  
  230.     //  
  231.     /**  
  232.      * Returns the alphabetic name of current registered operator. 
  233.      * <p> 
  234.      * Availability: Only when user is registered to a network 
  235.      */  
  236.     public String getNetworkOperatorName() {  
  237.         return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ALPHA);  
  238.     }  
  239.     /**  
  240.      * Returns the numeric name (MCC+MNC) of current registered operator. 
  241.      * <p> 
  242.      * Availability: Only when user is registered to a network 
  243.      */  
  244.     public String getNetworkOperator() {  
  245.         return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_NUMERIC);  
  246.     }  
  247.     /**   
  248.      * Returns true if the device is considered roaming on the current 
  249.      * network, for GSM purposes. 
  250.      * <p> 
  251.      * Availability: Only when user registered to a network 
  252.      */  
  253.     public boolean isNetworkRoaming() {  
  254.         return "true".equals(SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISROAMING));  
  255.     }  
  256.     /**  
  257.      * Returns the ISO country code equivilent of the current registered 
  258.      * operator's MCC (Mobile Country Code). 
  259.      * <p> 
  260.      * Availability: Only when user is registered to a network 
  261.      */  
  262.     public String getNetworkCountryIso() {  
  263.         return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY);  
  264.     }  
  265.     /** Network type is unknown */  
  266.     public static final int NETWORK_TYPE_UNKNOWN = 0;  
  267.     /** Current network is GPRS */  
  268.     public static final int NETWORK_TYPE_GPRS = 1;  
  269.     /** Current network is EDGE */  
  270.     public static final int NETWORK_TYPE_EDGE = 2;  
  271.     /** Current network is UMTS */  
  272.     public static final int NETWORK_TYPE_UMTS = 3;  
  273.     /** 
  274.      * Returns a constant indicating the radio technology (network type)  
  275.      * currently in use on the device. 
  276.      * @return the network type 
  277.      * 
  278.      * @see #NETWORK_TYPE_UNKNOWN 
  279.      * @see #NETWORK_TYPE_GPRS 
  280.      * @see #NETWORK_TYPE_EDGE 
  281.      * @see #NETWORK_TYPE_UMTS 
  282.      */  
  283.     public int getNetworkType() {  
  284.         String prop = SystemProperties.get(TelephonyProperties.PROPERTY_DATA_NETWORK_TYPE);  
  285.         if ("GPRS".equals(prop)) {  
  286.             return NETWORK_TYPE_GPRS;  
  287.         }  
  288.         else if ("EDGE".equals(prop)) {  
  289.             return NETWORK_TYPE_EDGE;  
  290.         }  
  291.         else if ("UMTS".equals(prop)) {  
  292.             return NETWORK_TYPE_UMTS;  
  293.         }  
  294.         else {  
  295.             return NETWORK_TYPE_UNKNOWN;  
  296.         }  
  297.     }  
  298.     /** 
  299.      * Returns a string representation of the radio technology (network type) 
  300.      * currently in use on the device. 
  301.      * @return the name of the radio technology 
  302.      * 
  303.      * @hide pending API council review 
  304.      */  
  305.     public String getNetworkTypeName() {  
  306.         switch (getNetworkType()) {  
  307.             case NETWORK_TYPE_GPRS:  
  308.                 return "GPRS";  
  309.             case NETWORK_TYPE_EDGE:  
  310.                 return "EDGE";  
  311.             case NETWORK_TYPE_UMTS:  
  312.                 return "UMTS";  
  313.             default:  
  314.                 return "UNKNOWN";  
  315.         }  
  316.     }  
  317.     //  
  318.     //  
  319.     // SIM Card  
  320.     //  
  321.     //  
  322.     /** SIM card state: Unknown. Signifies that the SIM is in transition 
  323.      *  between states. For example, when the user inputs the SIM pin 
  324.      *  under PIN_REQUIRED state, a query for sim status returns  
  325.      *  this state before turning to SIM_STATE_READY. */  
  326.     public static final int SIM_STATE_UNKNOWN = 0;  
  327.     /** SIM card state: no SIM card is available in the device */  
  328.     public static final int SIM_STATE_ABSENT = 1;  
  329.     /** SIM card state: Locked: requires the user's SIM PIN to unlock */  
  330.     public static final int SIM_STATE_PIN_REQUIRED = 2;  
  331.     /** SIM card state: Locked: requires the user's SIM PUK to unlock */  
  332.     public static final int SIM_STATE_PUK_REQUIRED = 3;  
  333.     /** SIM card state: Locked: requries a network PIN to unlock */  
  334.     public static final int SIM_STATE_NETWORK_LOCKED = 4;  
  335.     /** SIM card state: Ready */  
  336.     public static final int SIM_STATE_READY = 5;  
  337.       
  338.     /**  
  339.      * Returns a constant indicating the state of the  
  340.      * device SIM card. 
  341.      *  
  342.      * @see #SIM_STATE_UNKNOWN 
  343.      * @see #SIM_STATE_ABSENT 
  344.      * @see #SIM_STATE_PIN_REQUIRED 
  345.      * @see #SIM_STATE_PUK_REQUIRED 
  346.      * @see #SIM_STATE_NETWORK_LOCKED 
  347.      * @see #SIM_STATE_READY 
  348.      */  
  349.     public int getSimState() {  
  350.         String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE);  
  351.         if ("ABSENT".equals(prop)) {  
  352.             return SIM_STATE_ABSENT;  
  353.         }  
  354.         else if ("PIN_REQUIRED".equals(prop)) {  
  355.             return SIM_STATE_PIN_REQUIRED;  
  356.         }  
  357.         else if ("PUK_REQUIRED".equals(prop)) {  
  358.             return SIM_STATE_PUK_REQUIRED;  
  359.         }  
  360.         else if ("NETWORK_LOCKED".equals(prop)) {  
  361.             return SIM_STATE_NETWORK_LOCKED;  
  362.         }  
  363.         else if ("READY".equals(prop)) {  
  364.             return SIM_STATE_READY;  
  365.         }  
  366.         else {  
  367.             return SIM_STATE_UNKNOWN;  
  368.         }  
  369.     }  
  370.     /**  
  371.      * Returns the MCC+MNC (mobile country code + mobile network code) of the 
  372.      * provider of the SIM. 5 or 6 decimal digits. 
  373.      * <p> 
  374.      * Availability: SIM state must be {@link #SIM_STATE_READY} 
  375.      * 
  376.      * @see #getSimState 
  377.      */  
  378.     public String getSimOperator() {  
  379.         return SystemProperties.get(TelephonyProperties.PROPERTY_SIM_OPERATOR_NUMERIC);  
  380.     }  
  381.     /**  
  382.      * Returns the Service Provider Name (SPN).  
  383.      * <p> 
  384.      * Availability: SIM state must be {@link #SIM_STATE_READY} 
  385.      * 
  386.      * @see #getSimState 
  387.      */  
  388.     public String getSimOperatorName() {  
  389.         return SystemProperties.get(TelephonyProperties.PROPERTY_SIM_OPERATOR_ALPHA);  
  390.     }  
  391.     /**  
  392.      * Returns the ISO country code equivalent for the SIM provider's country code. 
  393.      */  
  394.     public String getSimCountryIso() {  
  395.         return SystemProperties.get(TelephonyProperties.PROPERTY_SIM_OPERATOR_ISO_COUNTRY);  
  396.     }  
  397.     /** 
  398.      * Returns the serial number of the SIM, if applicable. 
  399.      * <p> 
  400.      * Requires Permission:  
  401.      *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 
  402.      */  
  403.     public String getSimSerialNumber() {  
  404.         try {  
  405.             return getSubscriberInfo().getSimSerialNumber();  
  406.         } catch (RemoteException ex) {  
  407.         }  
  408.         return null;  
  409.     }  
  410.     //  
  411.     //  
  412.     // Subscriber Info  
  413.     //  
  414.     //  
  415.     /** 
  416.      * Returns the unique subscriber ID, for example, the IMSI for a GSM phone. 
  417.      * <p> 
  418.      * Requires Permission:  
  419.      *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 
  420.      */  
  421.     public String getSubscriberId() {  
  422.         try {  
  423.             return getSubscriberInfo().getSubscriberId();  
  424.         } catch (RemoteException ex) {  
  425.         }  
  426.         return null;  
  427.     }  
  428.     /** 
  429.      * Returns the phone number string for line 1, for example, the MSISDN  
  430.      * for a GSM phone. 
  431.      * <p> 
  432.      * Requires Permission:  
  433.      *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 
  434.      */  
  435.     public String getLine1Number() {  
  436.         try {  
  437.             return getSubscriberInfo().getLine1Number();  
  438.         } catch (RemoteException ex) {  
  439.         }  
  440.         return null;  
  441.     }  
  442.     /** 
  443.      * Returns the alphabetic identifier associated with the line 1 number.  
  444.      * <p> 
  445.      * Requires Permission:  
  446.      *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 
  447.      * @hide 
  448.      * nobody seems to call this. 
  449.      */  
  450.     public String getLine1AlphaTag() {  
  451.         try {  
  452.             return getSubscriberInfo().getLine1AlphaTag();  
  453.         } catch (RemoteException ex) {  
  454.         }  
  455.         return null;  
  456.     }  
  457.     /** 
  458.      * Returns the voice mail number. 
  459.      * <p> 
  460.      * Requires Permission:  
  461.      *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 
  462.      */  
  463.     public String getVoiceMailNumber() {  
  464.         try {  
  465.             return getSubscriberInfo().getVoiceMailNumber();  
  466.         } catch (RemoteException ex) {  
  467.         }  
  468.         return null;  
  469.     }  
  470.     /** 
  471.      * Retrieves the alphabetic identifier associated with the voice 
  472.      * mail number. 
  473.      * <p> 
  474.      * Requires Permission:  
  475.      *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 
  476.      */  
  477.     public String getVoiceMailAlphaTag() {  
  478.         try {  
  479.             return getSubscriberInfo().getVoiceMailAlphaTag();  
  480.         } catch (RemoteException ex) {  
  481.         }  
  482.         return null;  
  483.     }  
  484.     private IPhoneSubInfo getSubscriberInfo() {  
  485.         // get it each time because that process crashes a lot  
  486.         return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));  
  487.     }  
  488.     /** Device call state: No activity. */  
  489.     public static final int CALL_STATE_IDLE = 0;  
  490.     /** Device call state: Ringing. A new call arrived and is 
  491.      *  ringing or waiting. In the latter case, another call is  
  492.      *  already active. */  
  493.     public static final int CALL_STATE_RINGING = 1;  
  494.     /** Device call state: Off-hook. At least one call exists  
  495.       * that is dialing, active, or on hold, and no calls are ringing 
  496.       * or waiting. */  
  497.     public static final int CALL_STATE_OFFHOOK = 2;  
  498.     /** 
  499.      * Returns a constant indicating the call state (cellular) on the device. 
  500.      */  
  501.     public int getCallState() {  
  502.         try {  
  503.             return getITelephony().getCallState();  
  504.         } catch (RemoteException ex) {  
  505.             // the phone process is restarting.  
  506.             return CALL_STATE_IDLE;  
  507.         }  
  508.     }  
  509.     /** Data connection activity: No traffic. */  
  510.     public static final int DATA_ACTIVITY_NONE = 0x00000000;  
  511.     /** Data connection activity: Currently receiving IP PPP traffic. */  
  512.     public static final int DATA_ACTIVITY_IN = 0x00000001;  
  513.     /** Data connection activity: Currently sending IP PPP traffic. */  
  514.     public static final int DATA_ACTIVITY_OUT = 0x00000002;  
  515.     /** Data connection activity: Currently both sending and receiving 
  516.      *  IP PPP traffic. */  
  517.     public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT;  
  518.     /** 
  519.      * Returns a constant indicating the type of activity on a data connection 
  520.      * (cellular). 
  521.      * 
  522.      * @see #DATA_ACTIVITY_NONE 
  523.      * @see #DATA_ACTIVITY_IN 
  524.      * @see #DATA_ACTIVITY_OUT 
  525.      * @see #DATA_ACTIVITY_INOUT 
  526.      */  
  527.     public int getDataActivity() {  
  528.         try {  
  529.             return getITelephony().getDataActivity();  
  530.         } catch (RemoteException ex) {  
  531.             // the phone process is restarting.  
  532.             return DATA_ACTIVITY_NONE;  
  533.         }  
  534.     }  
  535.     /** Data connection state: Disconnected. IP traffic not available. */  
  536.     public static final int DATA_DISCONNECTED   = 0;  
  537.     /** Data connection state: Currently setting up a data connection. */  
  538.     public static final int DATA_CONNECTING     = 1;  
  539.     /** Data connection state: Connected. IP traffic should be available. */  
  540.     public static final int DATA_CONNECTED      = 2;  
  541.     /** Data connection state: Suspended. The connection is up, but IP  
  542.      * traffic is temporarily unavailable. For example, in a 2G network,  
  543.      * data activity may be suspended when a voice call arrives. */  
  544.     public static final int DATA_SUSPENDED      = 3;  
  545.     /** 
  546.      * Returns a constant indicating the current data connection state  
  547.      * (cellular). 
  548.      * 
  549.      * @see #DATA_DISCONNECTED 
  550.      * @see #DATA_CONNECTING 
  551.      * @see #DATA_CONNECTED 
  552.      * @see #DATA_SUSPENDED 
  553.      */  
  554.     public int getDataState() {  
  555.         try {  
  556.             return getITelephony().getDataState();  
  557.         } catch (RemoteException ex) {  
  558.             // the phone process is restarting.  
  559.             return DATA_DISCONNECTED;  
  560.         }  
  561.     }  
  562.     private ITelephony getITelephony() {  
  563.         return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));  
  564.     }  
  565.     //  
  566.     //  
  567.     // PhoneStateListener  
  568.     //  
  569.     //  
  570.     /** 
  571.      * Registers a listener object to receive notification of changes  
  572.      * in specified telephony states.  
  573.      * <p> 
  574.      * To register a listener, pass a {@link PhoneStateListener} 
  575.      * and specify at least one telephony state of interest in  
  576.      * the events argument.  
  577.      *  
  578.      * At registration, and when a specified telephony state 
  579.      * changes, the telephony manager invokes the appropriate  
  580.      * callback method on the listener object and passes the  
  581.      * current (udpated) values. 
  582.      * <p> 
  583.      * To unregister a listener, pass the listener object and set the 
  584.      * events argument to  
  585.      * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0). 
  586.      *  
  587.      * @param listener The {@link PhoneStateListener} object to register 
  588.      *                 (or unregister) 
  589.      * @param events The telephony state(s) of interest to the listener, 
  590.      *               as a bitwise-OR combination of {@link PhoneStateListener}  
  591.      *               LISTEN_ flags. 
  592.      */  
  593.     public void listen(PhoneStateListener listener, int events) {  
  594.         String pkgForDebug = mContext != null ? mContext.getPackageName() : "<unknown>";  
  595.         try {  
  596.             Boolean notifyNow = (getITelephony() != null);  
  597.             mRegistry.listen(pkgForDebug, listener.callback, events, notifyNow);  
  598.         } catch (RemoteException ex) {  
  599.             // system process dead  
  600.         }  
  601.     }  
  602. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值