gpt4 book ai didi

android - 如何连续获取网络类型?

转载 作者:行者123 更新时间:2023-11-30 03:32:59 26 4
gpt4 key购买 nike

我目前正在研究项目查看器应用程序参数 2G 和 3G。
我想显示 rxqual 和 rx level,当检测到 3G 网络时,Rx Level rxQual 会变成 Ec/N0 和 RSSI,但是我得到了问题,我使用
getnetworktype()结合networktype = getNetworkTypeString(tm.getNetworkType());但不想改变获取的组织对应的网络类型,所以RxQual和RxLev不会变成Ec/N0和RSSI,请检查我的代码是否有遗漏或错误..
这是我的代码:

 public class MainActivity extends Activity 
{
//private static final Logger logger = LoggerFactory.getLogger;
protected String APP_NAME;
LogWriter lw;
PhoneStateListener myPhoneStateListener;
int cid, lac, mcc, mnc,kuatlevel,kualitas,kw3g;
String operator, networktype, networkOperator, type, cellinfo;

GsmCellLocation location;
TelephonyManager tm;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//((Object) PropertyConfigurator.getConfigurator(this)).configure();

tm = (TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(phoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION
|PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

location =((GsmCellLocation)tm.getCellLocation());
operator = tm.getNetworkOperatorName();
networkOperator = tm.getNetworkOperator();
if (networkOperator !=null && networkOperator.length() > 0){
try {
mcc = Integer.parseInt(networkOperator.substring(0, 3));
mnc = Integer.parseInt(networkOperator.substring(3));
} catch (NumberFormatException e){`enter code here`
}
}


networktype = getNetworkTypeString(tm.getNetworkType());
List<NeighboringCellInfo> cellinfo = tm.getNeighboringCellInfo();
if (null != cellinfo){
for(NeighboringCellInfo info : cellinfo){
((TextView)findViewById(R.id.neighbor)).setText("CID:"+(info.getCid()& 0xffff) +
" LAC:"+(info.getLac()& 0xffff));
}
}


((TextView)findViewById(R.id.mnc)).setText("MNC: " + mnc);
((TextView)findViewById(R.id.mcc)).setText("MCC: " + mcc);
((TextView)findViewById(R.id.operatorname)).setText("Operator: " + operator);
//((TextView)findViewById(R.id.networkType)).setText("Network Type: " + networktype);
}


private final PhoneStateListener phoneStateListener = new PhoneStateListener()
{
public void onCellLocationChanged(CellLocation location) {
GsmCellLocation gsmLocation = (GsmCellLocation)location;
setTextViewText(R.id.lac,String.valueOf("LAC: " + (gsmLocation.getLac()& 0xffff)));
setTextViewText(R.id.cid,String.valueOf("CID: " + (gsmLocation.getCid()& 0xffff)));
setTextViewText(R.id.networkType,String.valueOf("Network Type: " + (networktype)));

}

public void onSignalStrengthsChanged(SignalStrength signalStrength){
kualitas = signalStrength.getGsmBitErrorRate();
kw3g = -1 * (signalStrength.getGsmBitErrorRate());
kuatlevel = -113 + 2 *(signalStrength.getGsmSignalStrength());

if (networktype == "2G"){
setTextViewText(R.id.rxq_ecno,String.valueOf("RxQ: " + (kualitas)));
setTextViewText(R.id.rxl_rssi,String.valueOf("RxL: " + (kuatlevel) + " dBm"));
} else {
setTextViewText(R.id.rxq_ecno,String.valueOf("EcNo: " + (kw3g) + " dB"));
setTextViewText(R.id.rxl_rssi,String.valueOf("RSSI: " + (kuatlevel) + " dBm"));
setTextViewText(R.id.arfcn_rscp,String.valueOf("RSCP: " + (kuatlevel + kw3g) + " dBm"));
}


}
};


private String getNetworkTypeString(int Ntype) {
type = "unknown";
switch (Ntype) {
case TelephonyManager.NETWORK_TYPE_EDGE:type = "2G"; break;
case TelephonyManager.NETWORK_TYPE_GPRS:type = "2G"; break;
case TelephonyManager.NETWORK_TYPE_UMTS:type = "3G"; break;
case TelephonyManager.NETWORK_TYPE_1xRTT:type = "2G"; break;
case TelephonyManager.NETWORK_TYPE_HSDPA:type = "3G"; break;
default:
type = "unknown"; break;
}
// TODO Auto-generated method stub
return type;
}

protected void setTextViewText(int id, String text) {
((TextView)findViewById(id)).setText(text);
// TODO Auto-generated method stub

}

//public static Logger getLogger() {
// return logger;
//}

}

最佳答案

使用 PhoneStateListener.LISTEN_SERVICE_STATE 标记并覆盖 onServiceStateChanged(ServiceState serviceState) ,如下所示:

@Override
public void onServiceStateChanged(ServiceState serviceState){
super.onServiceStateChanged(serviceState);
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int networkType = tm.getNetworkType();
}

每次网络类型更改时都会触发此函数。

关于android - 如何连续获取网络类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17139890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com