我想显示信号强度和cell id和lac。我为SIGNAL_STRENGTHS和CELL_LOCATION创建了一个监听器。在UMTS中连接时,我仍然无法得到正确的值(它的堆栈显示为-1),我无法显示单元格id和lac。我在侦听器类中添加了另一个方法来监听位置更改。我不知道它是否正确。 onCellLocationChanged()方法属于PhoneStateListener所以我为什么要这样做呢?谢谢
package com.example.gsmdata;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import android.telephony.TelephonyManager;
import android.telephony.SignalStrength;
import android.telephony.gsm.GsmCellLocation;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.PhoneStateListener;
public class GsmDataActivity extends Activity {
/** Called when the activity is first created. */
int network_type;
int signal_strength;
int cid;
int Lac;
int bts;
TextView network, signal, ci, lac;
TelephonyManager phone ;
GsmCellLocation CellId;
CdmaCellLocation baseStation;
SignalStrength phone_sig;
signalStateListener listenphone;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
network = (TextView) findViewById(R.id.tvNetwork);
signal =(TextView) findViewById(R.id.tvSignalValue);
ci = (TextView) findViewById(R.id.tvCiValue);
lac = (TextView) findViewById(R.id.tvLacValue);
/* Update Listener and start it */
phone = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
listenphone= new signalStateListener();
phone.listen(listenphone, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS | PhoneStateListener.LISTEN_CELL_LOCATION);
network_type = phone.getNetworkType();
switch (network_type){
case 1: network.setText("GPRS");break;
case 3: network.setText("UMTS");break;
case 4: network.setText("CDMA");break;
case 8: network.setText("HSDPA");break;
case 9: network.setText("HSUPA");break;
case 10: network.setText("HSPA");break;
case 13: network.setText("LTE");break;
default: network.setText("UNKNOWN");break;
}
}
@Override
protected void onPause()
{
super.onPause();
phone.listen(listenphone, PhoneStateListener.LISTEN_NONE);
}
@Override
protected void onResume()
{
super.onResume();
phone.listen(listenphone, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS |PhoneStateListener.LISTEN_CELL_LOCATION);
network_type = phone.getNetworkType();
switch (network_type){
case 1: network.setText("GPRS");break;
case 3: network.setText("UMTS");break;
case 4: network.setText("CDMA");break;
case 8: network.setText("HSDPA");break;
case 9: network.setText("HSUPA");break;
case 10: network.setText("HSPA");break;
case 13: network.setText("LTE");break;
default: network.setText("UNKNOWN");break;
}
}
private class signalStateListener extends PhoneStateListener{
@Override
public void onSignalStrengthsChanged(SignalStrength phone_sig){
super.onSignalStrengthsChanged(phone_sig);
if (network_type==1){
signal_strength = phone_sig.getGsmSignalStrength();
signal.setText(-1*(113-(2*signal_strength)) + "dBm");
}
else if(network_type==0){
signal.setText("Unknown Network type");
}
else{
signal_strength= phone_sig.getCdmaDbm();
signal.setText(signal_strength + "dBm");
}
}
}
public void onCellLocationChanged(GsmCellLocation CellId){
super.onCellLocationChanged(CellId);
ci.setText(CellId.getCid());
lac.setText(CellId.getLac());
/*bts= baseStation.getBaseStationId();
Lac=cellId.getLac();*/
}
}
答案 0 :(得分:0)
我发现代码中缺少了什么。
CellId=(GsmCellLocation) phone.getCellLocation();
如果我添加这行代码一切正常,我可以看到Cell Id和基站的LAC