Andoid 获取信号强度

  1.获得TelephoneManager: 

TelephonyManager telephoneManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);


  2.获得PhoneStateListener:

final int type = telephoneManager.getNetworkType();
PhoneStateListener phoneStateListener = new PhoneStateListener() {

			@Override
			public void onSignalStrengthsChanged(SignalStrength signalStrength) {
				// TODO Auto-generated method stub
				super.onSignalStrengthsChanged(signalStrength);
				StringBuffer sb = new StringBuffer();
				String strength = String.valueOf(signalStrength
						.getGsmSignalStrength());
				if (type == TelephonyManager.NETWORK_TYPE_UMTS
						|| type == TelephonyManager.NETWORK_TYPE_HSDPA) {
					sb.append("联通3g").append("信号强度:").append(strength);
				} else if (type == TelephonyManager.NETWORK_TYPE_GPRS
						|| type == TelephonyManager.NETWORK_TYPE_EDGE) {
					sb.append("移动或者联通2g").append("信号强度:").append(strength);
				}else if(type==TelephonyManager.NETWORK_TYPE_CDMA){
					sb.append("电信2g").append("信号强度:").append(strength);
				}else if(type==TelephonyManager.NETWORK_TYPE_EVDO_0
						||type==TelephonyManager.NETWORK_TYPE_EVDO_A){
					sb.append("电信3g").append("信号强度:").append(strength);
					
				}else{
					sb.append("非以上信号").append("信号强度:").append(strength);
				}

				toast.setText(sb.toString());
				toast.show();
			}

		};

 对于上面代码的解释:

      通过telePhoneManager获得网络类型,源码中给出的网络类型有以下:

    /** 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;
    /** Current network is CDMA: Either IS95A or IS95B*/
    public static final int NETWORK_TYPE_CDMA = 4;
    /** Current network is EVDO revision 0*/
    public static final int NETWORK_TYPE_EVDO_0 = 5;
    /** Current network is EVDO revision A*/
    public static final int NETWORK_TYPE_EVDO_A = 6;
    /** Current network is 1xRTT*/
    public static final int NETWORK_TYPE_1xRTT = 7;
    /** Current network is HSDPA */
    public static final int NETWORK_TYPE_HSDPA = 8;
    /** Current network is HSUPA */
    public static final int NETWORK_TYPE_HSUPA = 9;
    /** Current network is HSPA */
    public static final int NETWORK_TYPE_HSPA = 10;
    /** Current network is iDen */
    public static final int NETWORK_TYPE_IDEN = 11;
    /** Current network is EVDO revision B*/
    public static final int NETWORK_TYPE_EVDO_B = 12;


在中国,联通3g为UMTS或者HSDPA,电信的3g为EVDO,移动和联通的2g为GPRS或者EGDE,电信的2G为CMDA

onSignalStrengthsChanged()方法,当信号改变时回调

 

 3.注册监听到telephoneManager:

telephoneManager.listen(phoneStateListener,
				PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);


 4.在manifest中获得权限


 

你可能感兴趣的:(android)