1
我想顯示的信號強度和單元格id和lac。我爲SIGNAL_STRENGTHS和CELL_LOCATION創建了一個監聽器。在UMTS(其堆棧顯示-1)連接時,我仍然無法獲得適當的值,我無法顯示cell id和lac。我在偵聽器類中添加了另一個方法來偵聽位置變化。我不知道它是否正確。 onCellLocationChanged()方法屬於PhoneStateListener,所以我爲什麼要這樣做?感謝該方法cellLocationChanged未定義的類型活動
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();*/
}
}
其與LTE和UMTS類型的網絡爲什麼會這樣,什麼工作是解決方案的LAC – Ateeq 2013-07-30 08:22:31
@Ateeq http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onCellInfoChanged(java.util.List) –
black
2014-01-20 12:01:34