android 3G 手机信号 信息获取

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

[java] 
TelephonyManager tel = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 

TelephonyManager 可以监听 PhoneStateListener, PhoneStateListener 提供了
[java] 
onSignalStrengthsChanged   手机信号变动 
onServiceStateChanged      手机服务状态变动 

其中onSignalStrengthsChanged可以获取到的有用信息如下:
[java]
/*
  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 提供的状态变动如下:

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

以下给出具体的实例:

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

[html] 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"  
    android:gravity="center" 
    android:orientation="vertical"> 
 
    <ImageButton 
        android:id="@+id/Icon_3GStatus" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_3g"/> 
     
    <TextView 
        android:id="@+id/Label_3GDetail" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:maxWidth="320dp"/> 
 
</LinearLayout> 

2. 为3G图标添加一组可选的图标,在drawable-hdpi目录下增加ic_3g.xml文件,内容如下:
[html] 
<?xml version="1.0" encoding="utf-8"?> 
 
<level-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:maxLevel="60"  android:drawable="@drawable/ic_3g_s3" /> 
    <item android:maxLevel="90"  android:drawable="@drawable/ic_3g_s2" /> 
    <item android:maxLevel="96"  android:drawable="@drawable/ic_3g_s1" /> 
    <item android:maxLevel="100" android:drawable="@drawable/ic_3g_s0" /> 
</level-list> 

注意需要导入用到的图片

3.在代码中监听手机信号和状态的变动  (G3Example.java)
[java]
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
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 


 

你可能感兴趣的:(android 3G 手机信号 信息获取)