這個類是很有用地,可以得到很多關于手機的信息,做應用時必須的工具。
不廢話,直接上注釋后的代碼,請享用:
<code>
/**
*
*@author?dingran
* 創建日期? 2010-4-29? 下午 05:02:47
*
*/
package?net.sunniwell.app;
import?android.app.Activity;
import?android.os.Bundle;
import?android.telephony.CellLocation;
import?android.telephony.PhoneStateListener;
import?android.telephony.TelephonyManager;
public?class?TelManager?extends?Activity?{
?
@Override
protected?void?onCreate(Bundle?savedInstanceState)?{
??super.onCreate(savedInstanceState);
??TelephonyManager?tm?=?(TelephonyManager)?this.getSystemService(TELEPHONY_SERVICE);
??
??/*
???*? 電話狀態:
???*?1.tm.CALL_STATE_IDLE=0?????????? 無活動
???*?2.tm.CALL_STATE_RINGING=1?? 響鈴
???*?3.tm.CALL_STATE_OFFHOOK=2?? 摘機
???*/
??tm.getCallState();//int
??
??/*
???*? 電話方位:
???*?
???*/
??tm.getCellLocation();//CellLocation
??
??/*
???*? 唯一的設備 ID :
???*?GSM 手機的? IMEI? 和? CDMA 手機的? MEID.?
???*?Return?null?if?device?ID?is?not?available.
???*/
??tm.getDeviceId();//String
??
??/*
???*? 設備的軟件版本號:
???*? 例如: the?IMEI/SV(software?version)?for?GSM?phones.
???*?Return?null?if?the?software?version?is?not?available.?
???*/
??tm.getDeviceSoftwareVersion();//String
??
??/*
???*? 手機號:
???*?GSM 手機的? MSISDN.
???*?Return?null?if?it?is?unavailable.?
???*/
??tm.getLine1Number();//String
??
??/*
???*? 附近的電話的信息 :
???*? 類型: List<NeighboringCellInfo>?
???*? 需要權限: android.Manifest.permission#ACCESS_COARSE_UPDATES
???*/
??tm.getNeighboringCellInfo();//List<NeighboringCellInfo>
??
??/*
???*? 獲取 ISO 標準的國家碼,即國際長途區號。
???*? 注意:僅當用戶已在網絡注冊后有效。
???*??????? 在 CDMA 網絡中結果也許不可靠。
???*/
??tm.getNetworkCountryIso();//String
??
??/*
???*?MCC+MNC(mobile?country?code?+?mobile?network?code)
???*? 注意:僅當用戶已在網絡注冊時有效。
???*???? 在 CDMA 網絡中結果也許不可靠。
???*/
??tm.getNetworkOperator();//String
??
??/*
???*? 按照字母次序的 current?registered?operator( 當前已注冊的用戶 ) 的名字
???*? 注意:僅當用戶已在網絡注冊時有效。
???*???? 在 CDMA 網絡中結果也許不可靠。
???*/
??tm.getNetworkOperatorName();//String
??
??/*
???*? 當前使用的網絡類型:
???*? 例如:? NETWORK_TYPE_UNKNOWN?? 網絡類型未知?? 0
?????NETWORK_TYPE_GPRS?????GPRS 網絡?? 1
?????NETWORK_TYPE_EDGE?????EDGE 網絡?? 2
?????NETWORK_TYPE_UMTS?????UMTS 網絡?? 3
?????NETWORK_TYPE_HSDPA????HSDPA 網絡?? 8?
?????NETWORK_TYPE_HSUPA????HSUPA 網絡?? 9
?????NETWORK_TYPE_HSPA?????HSPA 網絡?? 10
?????NETWORK_TYPE_CDMA?????CDMA 網絡 ,IS95A? 或? IS95B.??4
?????NETWORK_TYPE_EVDO_0???EVDO 網絡 ,?revision?0.??5
?????NETWORK_TYPE_EVDO_A???EVDO 網絡 ,?revision?A.??6
?????NETWORK_TYPE_1xRTT????1xRTT 網絡?? 7
???*/
??tm.getNetworkType();//int
??
??/*
???*? 手機類型:
???*? 例如:? PHONE_TYPE_NONE?? 無信號
?????PHONE_TYPE_GSM???GSM 信號
?????PHONE_TYPE_CDMA??CDMA 信號
???*/
??tm.getPhoneType();//int
??
??/*
???*?Returns?the?ISO?country?code?equivalent?for?the?SIM?provider's?country?code.
???*? 獲取 ISO 國家碼,相當于提供 SIM 卡的國家碼。
???*?
???*/
??tm.getSimCountryIso();//String
??
??/*
???*?Returns?the?MCC+MNC?(mobile?country?code?+?mobile?network?code)?of?the?provider?of?the?SIM.?5?or?6?decimal?digits.
???*? 獲取 SIM 卡提供的移動國家碼和移動網絡碼 .5 或 6 位的十進制數字 .
???*?SIM 卡的狀態必須是? SIM_STATE_READY( 使用 getSimState() 判斷 ).
???*/
??tm.getSimOperator();//String
??
??/*
???*? 服務商名稱:
???*? 例如:中國移動、聯通
???*?SIM 卡的狀態必須是? SIM_STATE_READY( 使用 getSimState() 判斷 ).
???*/
??tm.getSimOperatorName();//String
??
??/*
???*?SIM 卡的序列號:
???*? 需要權限: READ_PHONE_STATE
???*/
??tm.getSimSerialNumber();//String
??
??/*
???*?SIM 的狀態信息:
???*??SIM_STATE_UNKNOWN?????????? 未知狀態? 0
???SIM_STATE_ABSENT??????????? 沒插卡? 1
???SIM_STATE_PIN_REQUIRED????? 鎖定狀態,需要用戶的 PIN 碼解鎖? 2
???SIM_STATE_PUK_REQUIRED????? 鎖定狀態,需要用戶的 PUK 碼解鎖? 3
???SIM_STATE_NETWORK_LOCKED??? 鎖定狀態,需要網絡的 PIN 碼解鎖? 4
???SIM_STATE_READY???????????? 就緒狀態? 5
???*/
??tm.getSimState();//int
??
??/*
???*? 唯一的用戶 ID :
???*? 例如: IMSI( 國際移動用戶識別碼 )?for?a?GSM?phone.
???*? 需要權限: READ_PHONE_STATE
???*/
??tm.getSubscriberId();//String
??
??/*
???*? 取得和語音郵件相關的標簽,即為識別符
???*? 需要權限: READ_PHONE_STATE
???*/
??tm.getVoiceMailAlphaTag();//String
??
??/*
???*? 獲取語音郵件號碼:
???*? 需要權限: READ_PHONE_STATE
???*/
??tm.getVoiceMailNumber();//String
??
??/*
???*?ICC 卡是否存在
???*/
??tm.hasIccCard();//boolean
??
??/*
???*? 是否漫游 :
???*?( 在 GSM 用途下 )
???*/
??tm.isNetworkRoaming();//
??
??
??
}
?
?
}
<code/>
?
鑒于有些同志沒有源碼,我把源碼粘出來, TelephonyManager.java :
?
<code>
package?android.telephony;
?
import?com.android.internal.telephony.*;
?
import?java.util.ArrayList;
import?java.util.List;
?
import?android.annotation.SdkConstant.SdkConstantType;
import?android.annotation.SdkConstant;
import?android.content.Context;
import?android.os.Bundle;
import?android.os.RemoteException;
import?android.os.ServiceManager;
import?android.os.SystemProperties;
?
?
/**
*?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?acess?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.?
*/
public?class?TelephonyManager?{
????private?static?final?String?TAG?=?"TelephonyManager";
?
????private?Context?mContext;
????private?ITelephonyRegistry?mRegistry;
?
????/**?@hide?*/
????public?TelephonyManager(Context?context)?{
????????mContext?=?context;
????????mRegistry?=?ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
????????????????????"telephony.registry"));
????}
?
????/**?@hide?*/
????private?TelephonyManager()?{
????}
?
????private?static?TelephonyManager?sInstance?=?new?TelephonyManager();
?
????/**?@hide?*/
????public?static?TelephonyManager?getDefault()?{
????????return?sInstance;
????}
?
?
????//
????//?Broadcast?Intent?actions
????//
?
????/**
?????*?Broadcast?intent?action?indicating?that?the?call?state?(cellular)
?????*?on?the?device?has?changed.
?????*
?????*?<p>
?????*?The?{@link?#EXTRA_STATE}?extra?indicates?the?new?call?state.
?????*?If?the?new?state?is?RINGING,?a?second?extra
?????*?{@link?#EXTRA_INCOMING_NUMBER}?provides?the?incoming?phone?number?as
?????*?a?String.
?????*
?????*?<p?class="note">
?????*?Requires?the?READ_PHONE_STATE?permission.
?????*
?????*?<p?class="note">
?????*?This?was?a?{@link?android.content.Context#sendStickyBroadcast?sticky}
?????*?broadcast?in?version?1.0,?but?it?is?no?longer?sticky.
?????*?Instead,?use?{@link?#getCallState}?to?synchronously?query?the?current?call?state.
?????*
?????*?@see?#EXTRA_STATE
?????*?@see?#EXTRA_INCOMING_NUMBER
?????*?@see?#getCallState
?????*/
????@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
????public?static?final?String?ACTION_PHONE_STATE_CHANGED?=
????????????"android.intent.action.PHONE_STATE";
?
????/**
?????*?The?lookup?key?used?with?the?{@link?#ACTION_PHONE_STATE_CHANGED}?broadcast
?????*?for?a?String?containing?the?new?call?state.
?????*
?????*?@see?#EXTRA_STATE_IDLE
?????*?@see?#EXTRA_STATE_RINGING
?????*?@see?#EXTRA_STATE_OFFHOOK
?????*
?????*?<p?class="note">
?????*?Retrieve?with
?????*?{@link?android.content.Intent#getStringExtra(String)}.
?????*/
????public?static?final?String?EXTRA_STATE?=?Phone.STATE_KEY;
?
????/**
?????*?Value?used?with?{@link?#EXTRA_STATE}?corresponding?to
?????*?{@link?#CALL_STATE_IDLE}.
?????*/
????public?static?final?String?EXTRA_STATE_IDLE?=?Phone.State.IDLE.toString();
?
????/**
?????*?Value?used?with?{@link?#EXTRA_STATE}?corresponding?to
?????*?{@link?#CALL_STATE_RINGING}.
?????*/
????public?static?final?String?EXTRA_STATE_RINGING?=?Phone.State.RINGING.toString();
?
????/**
?????*?Value?used?with?{@link?#EXTRA_STATE}?corresponding?to
?????*?{@link?#CALL_STATE_OFFHOOK}.
?????*/
????public?static?final?String?EXTRA_STATE_OFFHOOK?=?Phone.State.OFFHOOK.toString();
?
????/**
?????*?The?lookup?key?used?with?the?{@link?#ACTION_PHONE_STATE_CHANGED}?broadcast
?????*?for?a?String?containing?the?incoming?phone?number.
?????*?Only?valid?when?the?new?call?state?is?RINGING.
?????*
?????*?<p?class="note">
?????*?Retrieve?with
?????*?{@link?android.content.Intent#getStringExtra(String)}.
?????*/
????public?static?final?String?EXTRA_INCOMING_NUMBER?=?"incoming_number";
?
?
????//
????//
????//?Device?Info
????//
????//
?
????/**
?????*?Returns?the?software?version?number?for?the?device,?for?example,?
?????*?the?IMEI/SV?for?GSM?phones.
?????*
?????*?<p>Requires?Permission:?
?????*???{@link?android.Manifest.permission#READ_PHONE_STATE?READ_PHONE_STATE}
?????*/
????public?String?getDeviceSoftwareVersion()?{
????????try?{
????????????return?getSubscriberInfo().getDeviceSvn();
????????}?catch?(RemoteException?ex)?{
????????}
????????return?null;
????}
?
????/**
?????*?Returns?the?unique?device?ID,?for?example,the?IMEI?for?GSM
?????*?phones.
?????*
?????*?<p>Requires?Permission:?
?????*???{@link?android.Manifest.permission#READ_PHONE_STATE?READ_PHONE_STATE}
?????*/
????public?String?getDeviceId()?{
????????try?{
????????????return?getSubscriberInfo().getDeviceId();
????????}?catch?(RemoteException?ex)?{
????????}
????????return?null;
????}
?
????/**
?????*?Returns?the?current?location?of?the?device.
?????*
?????*?<p>Requires?Permission:?{@link?android.Manifest.permission#ACCESS_COARSE_LOCATION
?????*?ACCESS_COARSE_LOCATION}.
?????*/
????public?CellLocation?getCellLocation()?{
????????try?{
????????????Bundle?bundle?=?getITelephony().getCellLocation();
????????????return?CellLocation.newFromBundle(bundle);
????????}?catch?(RemoteException?ex)?{
????????}
????????return?null;
????}
?
????/**
?????*?Enables?location?update?notifications.??{@link?PhoneStateListener#onCellLocationChanged
?????*?PhoneStateListener.onCellLocationChanged}?will?be?called?on?location?updates.
?????*
?????*?<p>Requires?Permission:?{@link?android.Manifest.permission#CONTROL_LOCATION_UPDATES?
?????*?CONTROL_LOCATION_UPDATES}
?????*
?????*?@hide
?????*/
????public?void?enableLocationUpdates()?{
????????try?{
????????????getITelephony().enableLocationUpdates();
????????}?catch?(RemoteException?ex)?{
????????}
????}
?
????/**
?????*?Disables?location?update?notifications.??{@link?PhoneStateListener#onCellLocationChanged
?????*?PhoneStateListener.onCellLocationChanged}?will?be?called?on?location?updates.
?????*
?????*?<p>Requires?Permission:?{@link?android.Manifest.permission#CONTROL_LOCATION_UPDATES
?????*?CONTROL_LOCATION_UPDATES}
?????*
?????*?@hide
?????*/
????public?void?disableLocationUpdates()?{
????????try?{
????????????getITelephony().disableLocationUpdates();
????????}?catch?(RemoteException?ex)?{
????????}
????}
?
????/**
?????*?Returns?the?neighboring?cell?information?of?the?device.
?????*?
?????*?@return?List?of?NeighboringCellInfo?or?null?if?info?unavailable.
?????*?
?????*?<p>Requires?Permission:?
?????*?(@link?android.Manifest.permission#ACCESS_COARSE_UPDATES}
?????*/
????public?List<NeighboringCellInfo>?getNeighboringCellInfo()?{
???????try?{
???????????return?getITelephony().getNeighboringCellInfo();
???????}?catch?(RemoteException?ex)?{
???????}
???????return?null;
???????
????}
????
????/**
?????*?No?phone?module
?????*/
????public?static?final?int?PHONE_TYPE_NONE?=?0;
?
????/**
?????*?GSM?phone
?????*/
????public?static?final?int?PHONE_TYPE_GSM?=?1;
?
????/**
?????*?Returns?a?constant?indicating?the?device?phone?type.?
?????*?
?????*?@see?#PHONE_TYPE_NONE
?????*?@see?#PHONE_TYPE_GSM
?????*/
????public?int?getPhoneType()?{
????????//?in?the?future,?we?should?really?check?this
????????return?PHONE_TYPE_GSM;
????}
?
????//
????//?
????//?Current?Network
????//
????//
?
????/**?
?????*?Returns?the?alphabetic?name?of?current?registered?operator.
?????*?<p>
?????*?Availability:?Only?when?user?is?registered?to?a?network
?????*/
????public?String?getNetworkOperatorName()?{
????????return?SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ALPHA);
????}
?
????/**?
?????*?Returns?the?numeric?name?(MCC+MNC)?of?current?registered?operator.
?????*?<p>
?????*?Availability:?Only?when?user?is?registered?to?a?network
?????*/
????public?String?getNetworkOperator()?{
????????return?SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_NUMERIC);
????}
?
????/**??
?????*?Returns?true?if?the?device?is?considered?roaming?on?the?current
?????*?network,?for?GSM?purposes.
?????*?<p>
?????*?Availability:?Only?when?user?registered?to?a?network
?????*/
????public?boolean?isNetworkRoaming()?{
????????return?"true".equals(SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISROAMING));
????}
?
????/**?
?????*?Returns?the?ISO?country?code?equivilent?of?the?current?registered
?????*?operator's?MCC?(Mobile?Country?Code).
?????*?<p>
?????*?Availability:?Only?when?user?is?registered?to?a?network
?????*/
????public?String?getNetworkCountryIso()?{
????????return?SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY);
????}
?
????/**?Network?type?is?unknown?*/
????public?static?final?int?NETWORK_TYPE_UNKNOWN?=?0;
????/**?Current?network?is?GPRS?*/
????public?static?final?int?NETWORK_TYPE_GPRS?=?1;
????/**?Current?network?is?EDGE?*/
????public?static?final?int?NETWORK_TYPE_EDGE?=?2;
????/**?Current?network?is?UMTS?*/
????public?static?final?int?NETWORK_TYPE_UMTS?=?3;
?
????/**
?????*?Returns?a?constant?indicating?the?radio?technology?(network?type)?
?????*?currently?in?use?on?the?device.
?????*?@return?the?network?type
?????*
?????*?@see?#NETWORK_TYPE_UNKNOWN
?????*?@see?#NETWORK_TYPE_GPRS
?????*?@see?#NETWORK_TYPE_EDGE
?????*?@see?#NETWORK_TYPE_UMTS
?????*/
????public?int?getNetworkType()?{
????????String?prop?=?SystemProperties.get(TelephonyProperties.PROPERTY_DATA_NETWORK_TYPE);
????????if?("GPRS".equals(prop))?{
????????????return?NETWORK_TYPE_GPRS;
????????}
????????else?if?("EDGE".equals(prop))?{
????????????return?NETWORK_TYPE_EDGE;
????????}
????????else?if?("UMTS".equals(prop))?{
????????????return?NETWORK_TYPE_UMTS;
????????}
????????else?{
????????????return?NETWORK_TYPE_UNKNOWN;
????????}
????}
?
????/**
?????*?Returns?a?string?representation?of?the?radio?technology?(network?type)
?????*?currently?in?use?on?the?device.
?????*?@return?the?name?of?the?radio?technology
?????*
?????*?@hide?pending?API?council?review
?????*/
????public?String?getNetworkTypeName()?{
????????switch?(getNetworkType())?{
????????????case?NETWORK_TYPE_GPRS:
????????????????return?"GPRS";
????????????case?NETWORK_TYPE_EDGE:
????????????????return?"EDGE";
????????????case?NETWORK_TYPE_UMTS:
????????????????return?"UMTS";
????????????default:
????????????????return?"UNKNOWN";
????????}
????}
?
????//
????//
????//?SIM?Card
????//
????//
?
????/**?SIM?card?state:?Unknown.?Signifies?that?the?SIM?is?in?transition
?????*??between?states.?For?example,?when?the?user?inputs?the?SIM?pin
?????*??under?PIN_REQUIRED?state,?a?query?for?sim?status?returns?
?????*??this?state?before?turning?to?SIM_STATE_READY.?*/
????public?static?final?int?SIM_STATE_UNKNOWN?=?0;
????/**?SIM?card?state:?no?SIM?card?is?available?in?the?device?*/
????public?static?final?int?SIM_STATE_ABSENT?=?1;
????/**?SIM?card?state:?Locked:?requires?the?user's?SIM?PIN?to?unlock?*/
????public?static?final?int?SIM_STATE_PIN_REQUIRED?=?2;
????/**?SIM?card?state:?Locked:?requires?the?user's?SIM?PUK?to?unlock?*/
????public?static?final?int?SIM_STATE_PUK_REQUIRED?=?3;
????/**?SIM?card?state:?Locked:?requries?a?network?PIN?to?unlock?*/
????public?static?final?int?SIM_STATE_NETWORK_LOCKED?=?4;
????/**?SIM?card?state:?Ready?*/
????public?static?final?int?SIM_STATE_READY?=?5;
????
????/**?
?????*?Returns?a?constant?indicating?the?state?of?the?
?????*?device?SIM?card.
?????*?
?????*?@see?#SIM_STATE_UNKNOWN
?????*?@see?#SIM_STATE_ABSENT
?????*?@see?#SIM_STATE_PIN_REQUIRED
?????*?@see?#SIM_STATE_PUK_REQUIRED
?????*?@see?#SIM_STATE_NETWORK_LOCKED
?????*?@see?#SIM_STATE_READY
?????*/
????public?int?getSimState()?{
????????String?prop?=?SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE);
????????if?("ABSENT".equals(prop))?{
????????????return?SIM_STATE_ABSENT;
????????}
????????else?if?("PIN_REQUIRED".equals(prop))?{
????????????return?SIM_STATE_PIN_REQUIRED;
????????}
????????else?if?("PUK_REQUIRED".equals(prop))?{
????????????return?SIM_STATE_PUK_REQUIRED;
????????}
????????else?if?("NETWORK_LOCKED".equals(prop))?{
????????????return?SIM_STATE_NETWORK_LOCKED;
????????}
????????else?if?("READY".equals(prop))?{
????????????return?SIM_STATE_READY;
????????}
????????else?{
????????????return?SIM_STATE_UNKNOWN;
????????}
????}
?
????/**?
?????*?Returns?the?MCC+MNC?(mobile?country?code?+?mobile?network?code)?of?the
?????*?provider?of?the?SIM.?5?or?6?decimal?digits.
?????*?<p>
?????*?Availability:?SIM?state?must?be?{@link?#SIM_STATE_READY}
?????*
?????*?@see?#getSimState
?????*/
????public?String?getSimOperator()?{
????????return?SystemProperties.get(TelephonyProperties.PROPERTY_SIM_OPERATOR_NUMERIC);
????}
?
????/**?
?????*?Returns?the?Service?Provider?Name?(SPN).?
?????*?<p>
?????*?Availability:?SIM?state?must?be?{@link?#SIM_STATE_READY}
?????*
?????*?@see?#getSimState
?????*/
????public?String?getSimOperatorName()?{
????????return?SystemProperties.get(TelephonyProperties.PROPERTY_SIM_OPERATOR_ALPHA);
????}
?
????/**?
?????*?Returns?the?ISO?country?code?equivalent?for?the?SIM?provider's?country?code.
?????*/
????public?String?getSimCountryIso()?{
????????return?SystemProperties.get(TelephonyProperties.PROPERTY_SIM_OPERATOR_ISO_COUNTRY);
????}
?
????/**
?????*?Returns?the?serial?number?of?the?SIM,?if?applicable.
?????*?<p>
?????*?Requires?Permission:?
?????*???{@link?android.Manifest.permission#READ_PHONE_STATE?READ_PHONE_STATE}
?????*/
????public?String?getSimSerialNumber()?{
????????try?{
????????????return?getSubscriberInfo().getSimSerialNumber();
????????}?catch?(RemoteException?ex)?{
????????}
????????return?null;
????}
?
????//
????//
????//?Subscriber?Info
????//
????//
?
????/**
?????*?Returns?the?unique?subscriber?ID,?for?example,?the?IMSI?for?a?GSM?phone.
?????*?<p>
?????*?Requires?Permission:?
?????*???{@link?android.Manifest.permission#READ_PHONE_STATE?READ_PHONE_STATE}
?????*/
????public?String?getSubscriberId()?{
????????try?{
????????????return?getSubscriberInfo().getSubscriberId();
????????}?catch?(RemoteException?ex)?{
????????}
????????return?null;
????}
?
????/**
?????*?Returns?the?phone?number?string?for?line?1,?for?example,?the?MSISDN?
?????*?for?a?GSM?phone.
?????*?<p>
?????*?Requires?Permission:?
?????*???{@link?android.Manifest.permission#READ_PHONE_STATE?READ_PHONE_STATE}
?????*/
????public?String?getLine1Number()?{
????????try?{
????????????return?getSubscriberInfo().getLine1Number();
????????}?catch?(RemoteException?ex)?{
????????}
????????return?null;
????}
?
????/**
?????*?Returns?the?alphabetic?identifier?associated?with?the?line?1?number.?
?????*?<p>
?????*?Requires?Permission:?
?????*???{@link?android.Manifest.permission#READ_PHONE_STATE?READ_PHONE_STATE}
?????*?@hide
?????*?nobody?seems?to?call?this.
?????*/
????public?String?getLine1AlphaTag()?{
????????try?{
????????????return?getSubscriberInfo().getLine1AlphaTag();
????????}?catch?(RemoteException?ex)?{
????????}
????????return?null;
????}
?
????/**
?????*?Returns?the?voice?mail?number.
?????*?<p>
?????*?Requires?Permission:?
?????*???{@link?android.Manifest.permission#READ_PHONE_STATE?READ_PHONE_STATE}
?????*/
????public?String?getVoiceMailNumber()?{
????????try?{
????????????return?getSubscriberInfo().getVoiceMailNumber();
????????}?catch?(RemoteException?ex)?{
????????}
????????return?null;
????}
?
????/**
?????*?Retrieves?the?alphabetic?identifier?associated?with?the?voice
?????*?mail?number.
?????*?<p>
?????*?Requires?Permission:?
?????*???{@link?android.Manifest.permission#READ_PHONE_STATE?READ_PHONE_STATE}
?????*/
????public?String?getVoiceMailAlphaTag()?{
????????try?{
????????????return?getSubscriberInfo().getVoiceMailAlphaTag();
????????}?catch?(RemoteException?ex)?{
????????}
????????return?null;
????}
?
????private?IPhoneSubInfo?getSubscriberInfo()?{
????????//?get?it?each?time?because?that?process?crashes?a?lot
????????return?IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));
????}
?
?
????/**?Device?call?state:?No?activity.?*/
????public?static?final?int?CALL_STATE_IDLE?=?0;
????/**?Device?call?state:?Ringing.?A?new?call?arrived?and?is
?????*??ringing?or?waiting.?In?the?latter?case,?another?call?is?
?????*??already?active.?*/
????public?static?final?int?CALL_STATE_RINGING?=?1;
????/**?Device?call?state:?Off-hook.?At?least?one?call?exists?
??????*?that?is?dialing,?active,?or?on?hold,?and?no?calls?are?ringing
??????*?or?waiting.?*/
????public?static?final?int?CALL_STATE_OFFHOOK?=?2;
?
????/**
?????*?Returns?a?constant?indicating?the?call?state?(cellular)?on?the?device.
?????*/
????public?int?getCallState()?{
????????try?{
????????????return?getITelephony().getCallState();
????????}?catch?(RemoteException?ex)?{
????????????//?the?phone?process?is?restarting.
????????????return?CALL_STATE_IDLE;
????????}
????}
?
????/**?Data?connection?activity:?No?traffic.?*/
????public?static?final?int?DATA_ACTIVITY_NONE?=?0x00000000;
????/**?Data?connection?activity:?Currently?receiving?IP?PPP?traffic.?*/
????public?static?final?int?DATA_ACTIVITY_IN?=?0x00000001;
????/**?Data?connection?activity:?Currently?sending?IP?PPP?traffic.?*/
????public?static?final?int?DATA_ACTIVITY_OUT?=?0x00000002;
????/**?Data?connection?activity:?Currently?both?sending?and?receiving
?????*??IP?PPP?traffic.?*/
????public?static?final?int?DATA_ACTIVITY_INOUT?=?DATA_ACTIVITY_IN?|?DATA_ACTIVITY_OUT;
?
????/**
?????*?Returns?a?constant?indicating?the?type?of?activity?on?a?data?connection
?????*?(cellular).
?????*
?????*?@see?#DATA_ACTIVITY_NONE
?????*?@see?#DATA_ACTIVITY_IN
?????*?@see?#DATA_ACTIVITY_OUT
?????*?@see?#DATA_ACTIVITY_INOUT
?????*/
????public?int?getDataActivity()?{
????????try?{
????????????return?getITelephony().getDataActivity();
????????}?catch?(RemoteException?ex)?{
????????????//?the?phone?process?is?restarting.
????????????return?DATA_ACTIVITY_NONE;
????????}
????}
?
????/**?Data?connection?state:?Disconnected.?IP?traffic?not?available.?*/
????public?static?final?int?DATA_DISCONNECTED???=?0;
????/**?Data?connection?state:?Currently?setting?up?a?data?connection.?*/
????public?static?final?int?DATA_CONNECTING?????=?1;
????/**?Data?connection?state:?Connected.?IP?traffic?should?be?available.?*/
????public?static?final?int?DATA_CONNECTED??????=?2;
????/**?Data?connection?state:?Suspended.?The?connection?is?up,?but?IP?
?????*?traffic?is?temporarily?unavailable.?For?example,?in?a?2G?network,?
?????*?data?activity?may?be?suspended?when?a?voice?call?arrives.?*/
????public?static?final?int?DATA_SUSPENDED??????=?3;
?
????/**
?????*?Returns?a?constant?indicating?the?current?data?connection?state?
?????*?(cellular).
?????*
?????*?@see?#DATA_DISCONNECTED
?????*?@see?#DATA_CONNECTING
?????*?@see?#DATA_CONNECTED
?????*?@see?#DATA_SUSPENDED
?????*/
????public?int?getDataState()?{
????????try?{
????????????return?getITelephony().getDataState();
????????}?catch?(RemoteException?ex)?{
????????????//?the?phone?process?is?restarting.
????????????return?DATA_DISCONNECTED;
????????}
????}
?
????private?ITelephony?getITelephony()?{
????????return?ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
????}
?
????//
????//
????//?PhoneStateListener
????//
????//
?
????/**
?????*?Registers?a?listener?object?to?receive?notification?of?changes?
?????*?in?specified?telephony?states.?
?????*?<p>
?????*?To?register?a?listener,?pass?a?{@link?PhoneStateListener}
?????*?and?specify?at?least?one?telephony?state?of?interest?in?
?????*?the?events?argument.?
?????*?
?????*?At?registration,?and?when?a?specified?telephony?state
?????*?changes,?the?telephony?manager?invokes?the?appropriate?
?????*?callback?method?on?the?listener?object?and?passes?the?
?????*?current?(udpated)?values.
?????*?<p>
?????*?To?unregister?a?listener,?pass?the?listener?object?and?set?the
?????*?events?argument?to?
?????*?{@link?PhoneStateListener#LISTEN_NONE?LISTEN_NONE}?(0).
?????*?
?????*?@param?listener?The?{@link?PhoneStateListener}?object?to?register
?????*?????????????????(or?unregister)
?????*?@param?events?The?telephony?state(s)?of?interest?to?the?listener,
?????*???????????????as?a?bitwise-OR?combination?of?{@link?PhoneStateListener}?
?????*???????????????LISTEN_?flags.
?????*/
????public?void?listen(PhoneStateListener?listener,?int?events)?{
????????String?pkgForDebug?=?mContext?!=?null???mContext.getPackageName()?:?"<unknown>";
????????try?{
????????????Boolean?notifyNow?=?(getITelephony()?!=?null);
????????????mRegistry.listen(pkgForDebug,?listener.callback,?events,?notifyNow);
????????}?catch?(RemoteException?ex)?{
????????????//?system?process?dead
????????}
????}
}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
