android 3g狀態及信號監測

/*
 * tel.getNetworkOperator()
 3G中國是460固定的,
 中國移動的是 46000
 中國聯通的是 46001
 中國電信的是 46003
 *獲取國別
 tel.getSimCountryIso()
 */ 網絡

 public class My3GInfo {
  private WifiManager mWifi;
  private Context mContext;
  private Handler mHandler;
  private ImageView img;
  private String STRNetworkOperator[] = { "46000", "46001", "46003" };
  private int mark = -1;
  private int position;
  private int img3g[] = {};
  private boolean is3Ghave = false;
  private TelephonyManager tel;
  
  public My3GInfo(Context context, ImageView img) {
   mContext = context;
   this.img = img;
   init();
  } ui

  private void init() {
   // TODO Auto-generated method stub
   /* 可 參考一下相關的方法,獲得本身想要的參數來處理本身的ui
    public void initListValues(){           
            tel.getDeviceId());//獲取設備編號
            tel.getSimCountryIso());//獲取SIM卡國別
            tel.getSimSerialNumber());//獲取SIM卡序列號   
            (simState[tm.getSimState()]);//獲取SIM卡狀態
            (tel.getDeviceSoftwareVersion()!=null?tm.getDeviceSoftwareVersion():"未知"));    //獲取軟件版本
            tel.getNetworkOperator());//獲取網絡運營商代號
            tel.getNetworkOperatorName());//獲取網絡運營商名稱
            (phoneType[tm.getPhoneType()]);//獲取手機制式
            tel.getCellLocation().toString());//獲取設備當前位置
       }*/
   tel = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 
    
    firstView();   
    getmark();
     //設置監聽事件,監聽信號強度的改變和狀態的改變
   tel.listen(new PhoneStateMonitor(),
     PhoneStateListener.LISTEN_SIGNAL_STRENGTHS
       | PhoneStateListener.LISTEN_SERVICE_STATE);
  }
  private void getmark()//獲得當前電話卡的歸屬運營商
  {
   String strNetworkOperator = tel.getNetworkOperator();
   if (strNetworkOperator != null) {
    for (int i = 0; i < 3; i++) {
     if (strNetworkOperator.equals(STRNetworkOperator[i])) {
      mark = i;
      Log.v(TAG, "mark==" + i);
      break;
     }
    }
   } else {
    mark = -1;
   }
  }
  private void firstView() {//第一次自動檢測並設置初始狀態 this

   int state = tel.getSimState();
   switch (state) {
   case TelephonyManager.SIM_STATE_UNKNOWN:
    is3Ghave = false;
    break;
   case TelephonyManager.SIM_STATE_READY:
    is3Ghave = true;
    break;
   case TelephonyManager.SIM_STATE_ABSENT:
    is3Ghave = false;
    break;
   default:
    break;
   }  
  } 事件

  private String TAG = "WHLOG";
  int signal;
  class PhoneStateMonitor extends PhoneStateListener {
   public void onSignalStrengthsChanged(SignalStrength signalStrength) {//3g信號強度的改變
    super.onSignalStrengthsChanged(signalStrength);
    /*
     * signalStrength.isGsm() 是否GSM信號 2G or 3G
     * signalStrength.getCdmaDbm(); 聯通3G 信號強度
     * signalStrength.getCdmaEcio(); 聯通3G 載幹比
     * signalStrength.getEvdoDbm(); 電信3G 信號強度
     * signalStrength.getEvdoEcio(); 電信3G 載幹比
     * signalStrength.getEvdoSnr(); 電信3G 信噪比
     * signalStrength.getGsmSignalStrength(); 2G 信號強度
     * signalStrength.getGsmBitErrorRate(); 2G 誤碼率    
     * 載幹比 ,它是指空中模擬電波中的信號與噪聲的比值
     */
    Log.v(TAG, "change signal");
    if(mark<0)
    {
     getmark();
    }
    if (mark == 0) {
     signal = signalStrength.getGsmSignalStrength();
    } else if (mark == 1) {
     signal = signalStrength.getCdmaDbm();
    } else if (mark == 2) {
     signal = signalStrength.getEvdoDbm();
    }
    if(is3Ghave==true){
    getLevel();
    show3GDetail(position);
    }
   } ci

   private void getLevel() {
    // TODO Auto-generated method stub
    if (mark == 2) {//電信3g信號強度的分類,能夠按照ui自行劃分等級
     if (signal >= -65)
      position = 5;
     else if (signal >= -75)
      position = 4;
     else if (signal >= -85)
      position = 3;
     else if (signal >= -95)
      position = 2;
     else if (signal >= -105)
      position = 1;
     else
      position = 0;
    }
    if (mark == 1) {//聯通3g信號劃分
     if (signal >= -75)
      position = 5;
     else if (signal >= -80)
      position = 4;
     else if (signal >= -85)
      position = 3;
     else if (signal >= -95)
      position = 2;
     else if (signal >= -100)
      position = 1;
     else
      position = 0;
    }
    if (mark == 0) {//移動信號的劃分,這個不是很肯定是2g仍是3g
     if (signal <= 2 || signal == 99)
      position = 0;
     else if (signal >= 12)
      position = 5;
     else if (signal >= 10)
      position = 4;
     else if (signal >= 8)
      position = 3;
     else if (signal >= 5)
      position = 2;
     else
      position = 1;
    }
   } get

   public void onServiceStateChanged(ServiceState serviceState) {//3g狀態的改變
    super.onServiceStateChanged(serviceState);
    /*
     * ServiceState.STATE_EMERGENCY_ONLY 僅限緊急呼叫
     * ServiceState.STATE_IN_SERVICE 信號正常
     * ServiceState.STATE_OUT_OF_SERVICE 不在服務區
     * ServiceState.STATE_POWER_OFF 斷電
     */
    int pos = serviceState.getState();
    Log.v(TAG, "state change===" + pos); it

    switch (pos) {
    case ServiceState.STATE_EMERGENCY_ONLY:
     is3Ghave=true;
     show3GDetail(0);
     break;
    case ServiceState.STATE_IN_SERVICE:
     is3Ghave=true;  
     show3GDetail(3);
     break;
    case ServiceState.STATE_OUT_OF_SERVICE:
     is3Ghave=false;
     show3GDetail(6);
     break;
    case ServiceState.STATE_POWER_OFF:
     is3Ghave=false;
     show3GDetail(6);
     break;
    default:
     break;
    }
   }
  } io

  private void show3GDetail(int level) {//設置顯示3g的圖標
   // TODO Auto-generated method stub
   if(is3Ghave==false)
   {
    img.setBackgroundResource(img3g[6]);
   }else{
   img.setBackgroundResource(img3g[level]);
   }
  }
 } class

相關文章
相關標籤/搜索