Cellid LAC(获取)监听

Android 2.2之前的老API已经废除了:

com.android.internal.telephony 
Class PhoneStateIntentReceiver

java.lang.Object
  extended by android.content.BroadcastReceiver
      extended by com.android.internal.telephony.PhoneStateIntentReceiver

Deprecated.

@Deprecated
public final class PhoneStateIntentReceiver
   
   
    
    extends 
    
    BroadcastReceiver
   
   

DO NOT USE THIS CLASS: Use android.telephony.TelephonyManager and PhoneStateListener instead.


最新的API使用PhoneStatListener方式:


1. 继承PhoneStateListener, 重载onCellLocationChanged,onSignalStrengthsChanged,onCellInfoChanged,这这些回调了取到你想要的信息

 

 private class PhoneStateChangeListener extends PhoneStateListener {
    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
      String network = getNetwork();
      if (network.equals(NETWORK_TYPES[TelephonyManager.NETWORK_TYPE_CDMA])) {
        setCurrentRssi(signalStrength.getCdmaDbm());
      } else if (network.equals(NETWORK_TYPES[TelephonyManager.NETWORK_TYPE_EVDO_0]) ||
          network.equals(NETWORK_TYPES[TelephonyManager.NETWORK_TYPE_EVDO_A]) ||
            network.equals(NETWORK_TYPES[TelephonyManager.NETWORK_TYPE_EVDO_B])) {
        setCurrentRssi(signalStrength.getEvdoDbm());
      } else if (signalStrength.isGsm()) {
    	 //XXX 待研究,为什么联通网络我们会走这里,取到了GSM的signalStrenght(0,99),
    	 //而其它工具却能取到-84dBm(-100, 0)
    	  //各种网络信号强度术语:http://stackoverflow.com/questions/15054498/android-signalstrength-terminology-explanation
        setCurrentRssi(signalStrength.getGsmSignalStrength());
      }
    }
    
    @Override
    public void onCellInfoChanged (List<CellInfo> cellInfo) {
    	for(CellInfo info: cellInfo)
    	{
    		Logger.d(info.toString());
    	}
    }
    
   @Override
    public void onCellLocationChanged (CellLocation location) {
	   Logger.d(location.toString());
	   
       int type = telephonyManager.getNetworkType();  
       Logger.d("getCellIDInfo-->         NetworkType = " + type);  
       int phoneType = telephonyManager.getPhoneType();  
       Logger.d("getCellIDInfo-->         phoneType = " + phoneType);  
         
       if (type == TelephonyManager.NETWORK_TYPE_GPRS              // GSM网  
               || type == TelephonyManager.NETWORK_TYPE_EDGE  
               || type == TelephonyManager.NETWORK_TYPE_HSDPA)  {
    	   GsmCellLocation gsm = (GsmCellLocation)location;
    	   int lac = gsm.getLac(); 
           int cid = gsm.getCid();  
           //String mcc = telephonyManager.getNetworkOperator().substring(0, 3);  
           //String mnc = telephonyManager.getNetworkOperator().substring(3, 5);  
       }
       else if (type == TelephonyManager.NETWORK_TYPE_CDMA        // 电信cdma网  
               || type == TelephonyManager.NETWORK_TYPE_1xRTT  
               || type == TelephonyManager.NETWORK_TYPE_EVDO_0  
               || type == TelephonyManager.NETWORK_TYPE_EVDO_A)  
       {  
    	   CdmaCellLocation cdma = (CdmaCellLocation)location;
    	   int lac = cdma.getNetworkId();  
           int cid = cdma.getBaseStationId();      	   
           //String mcc = telephonyManager.getNetworkOperator().substring(0, 3);  
           //String mnc = String.valueOf(cdma.getSystemId());
       }
    }
  }

2. 向注册TelephonyManager注册监听事件

  /**
   * This method must be called in the service thread, as the system will create a Looper in
   * the calling thread which will handle the callbacks.
   */
  public void registerPhoneStateChangeListener() {
    initNetwork();
    
    if(null == phoneStateChangeListener) {
    	phoneStateChangeListener = new PhoneStateChangeListener();
    }
    telephonyManager.listen(phoneStateChangeListener,  PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    telephonyManager.listen(phoneStateChangeListener,  PhoneStateListener.LISTEN_CELL_INFO);
    telephonyManager.listen(phoneStateChangeListener,  PhoneStateListener.LISTEN_CELL_LOCATION);
  }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值