android 3G 手机信号 信息获取

转于:http://blog.csdn.net/hanvy_xia/article/details/8003185

-----------------------------------------------------------------------------------------

在androi中手机信号信息的获取可以通过系统提供的TELEPHONY_SERVICE获取

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


TelephonyManager 可以监听 PhoneStateListener, PhoneStateListener 提供了

onSignalStrengthsChanged   手机信号变动  
onServiceStateChanged      手机服务状态变动  


其中onSignalStrengthsChanged可以获取到的有用信息如下:

/* 
  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 误码率 
 
  载干比 ,它是指空中模拟电波中的信号与噪声的比值 
*/   

奇怪的是移动的3G信号TD-SCDMA 信息没办法获取到

而onServiceStateChanged 提供的状态变动如下:

/* 
  ServiceState.STATE_EMERGENCY_ONLY   仅限紧急呼叫 
  ServiceState.STATE_IN_SERVICE       信号正常 
  ServiceState.STATE_OUT_OF_SERVICE   不在服务区 
  ServiceState.STATE_POWER_OFF        断电 
*/  

以下给出具体的实例:

1.在布局文件中增加一个3G图标,和显示具体信息的文本  (activity_g3_example.xml)

package com.example.g3example;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.content.Context;  
import android.telephony.PhoneStateListener;  
import android.telephony.ServiceState;  
import android.telephony.SignalStrength;  
import android.telephony.TelephonyManager;  
import android.util.Log;  
import android.view.Menu;  
import android.widget.ImageButton;  
import android.widget.TextView;  
  
public class G3Example extends Activity {  
  
    private final String TAG = "G3Example";  
  
    private ImageButton       mIcon3G;  
    private TextView          mLabel3G;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_g3_example);  
  
        mIcon3G  = (ImageButton)findViewById(R.id.Icon_3GStatus);  
        mLabel3G = (TextView)findViewById(R.id.Label_3GDetail);   
  
        TelephonyManager tel = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);  
        tel.listen(new PhoneStateMonitor(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS | PhoneStateListener.LISTEN_SERVICE_STATE);  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        getMenuInflater().inflate(R.menu.activity_g3_example, menu);  
        return true;  
    }  
  
    public class PhoneStateMonitor extends PhoneStateListener{  
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {  
            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 误码率 
 
            载干比 ,它是指空中模拟电波中的信号与噪声的比值 
             */   
            mLabel3G.setText("IsGsm : " + signalStrength.isGsm() +   
                    "\nCDMA Dbm : " + signalStrength.getCdmaDbm() + "Dbm" +   
                    "\nCDMA Ecio : " + signalStrength.getCdmaEcio() +  "dB*10" +  
                    "\nEvdo Dbm : " + signalStrength.getEvdoDbm() + "Dbm" +   
                    "\nEvdo Ecio : " + signalStrength.getEvdoEcio() + "dB*10" +   
                    "\nGsm SignalStrength : " + signalStrength.getGsmSignalStrength() +   
                    "\nGsm BitErrorRate : " + signalStrength.getGsmBitErrorRate());  
  
            mIcon3G.setImageLevel(Math.abs(signalStrength.getGsmSignalStrength()));  
        }  
  
        public void onServiceStateChanged(ServiceState serviceState){  
            super.onServiceStateChanged(serviceState);  
  
            /* 
             ServiceState.STATE_EMERGENCY_ONLY   仅限紧急呼叫 
             ServiceState.STATE_IN_SERVICE       信号正常 
             ServiceState.STATE_OUT_OF_SERVICE   不在服务区 
             ServiceState.STATE_POWER_OFF        断电 
             */  
            switch(serviceState.getState())  
            {  
            case ServiceState.STATE_EMERGENCY_ONLY:  
                Log.d(TAG, "3G STATUS : STATE_EMERGENCY_ONLY");  
                break;  
            case ServiceState.STATE_IN_SERVICE:  
                Log.d(TAG, "3G STATUS : STATE_IN_SERVICE");  
                break;  
            case ServiceState.STATE_OUT_OF_SERVICE:  
                Log.d(TAG, "3G STATUS : STATE_OUT_OF_SERVICE");  
                break;  
            case ServiceState.STATE_POWER_OFF:  
                Log.d(TAG, "3G STATUS : STATE_POWER_OFF");  
                break;  
            default:  
                break;  
            }  
        }  
    }  
}  

4. 最后在AndroidManifest.xml增加获取手机信号权限的支持

[html] view plaincopy
 
  1. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  

相关推荐