在androi中手機信號信息的獲取能夠經過系統提供的TELEPHONY_SERVICE獲取 css
[java]
TelephonyManager tel = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); html
TelephonyManager 能夠監聽 PhoneStateListener, PhoneStateListener 提供了
[java]
onSignalStrengthsChanged 手機信號變更
onServiceStateChanged 手機服務狀態變更 java
其中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 信息沒辦法獲取到 android
而onServiceStateChanged 提供的狀態變更以下: app
[java]
/*
ServiceState.STATE_EMERGENCY_ONLY 僅限緊急呼叫
ServiceState.STATE_IN_SERVICE 信號正常
ServiceState.STATE_OUT_OF_SERVICE 不在服務區
ServiceState.STATE_POWER_OFF 斷電
*/ ide
如下給出具體的實例: 佈局
1.在佈局文件中增長一個3G圖標,和顯示具體信息的文本 (activity_g3_example.xml) 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> htm
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" />