Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何连续获得网络类型?_Android_Networking_Broadcastreceiver - Fatal编程技术网

Android 如何连续获得网络类型?

Android 如何连续获得网络类型?,android,networking,broadcastreceiver,Android,Networking,Broadcastreceiver,我目前正在研究project viewer应用程序参数2G和3G。 我想显示rxqual和rx级别,当3G网络上检测到网络时,rxqual和rx级别将变为Ec/N0和RSSI,但我发现问题,我使用 getnetworktype()与networktype=getNetworkTypeString(tm.getnetworktype())组合使用 但不想更改获得的相应组织的网络类型,因此RxQual和RxLev不会变成Ec/N0和RSSI,请检查我的代码是否有遗漏或错误。 这是我的代码: pub

我目前正在研究project viewer应用程序参数2G和3G。
我想显示rxqual和rx级别,当3G网络上检测到网络时,rxqual和rx级别将变为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;
        //}

}
公共类MainActivity扩展活动
{
//专用静态最终记录器Logger=LoggerFactory.getLogger;
受保护的字符串应用程序名称;
日志作者lw;
PhoneStateListener myPhoneStateListener;
国际cid、拉丁美洲和加勒比海、中冶、跨国公司、夸特维尔、夸利塔斯、kw3g;
字符串运算符,networktype,networkOperator,type,cellinfo;
GsmCellLocation位置;
TelephonyManager tm;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//((对象)PropertyConfigurator.getConfigurator(this)).configure();
tm=(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_服务);
tm.listen(phoneStateListener,phoneStateListener.listen\u单元格\u位置
|PhoneStateListener。倾听信号(强度);
location=((GsmCellLocation)tm.getCellLocation());
operator=tm.getNetworkOperatorName();
networkOperator=tm.getNetworkOperator();
if(networkOperator!=null&&networkOperator.length()>0){
试一试{
mcc=Integer.parseInt(networkOperator.substring(0,3));
mnc=Integer.parseInt(networkOperator.substring(3));
}catch(numberformatxe){`在这里输入代码`
}
}
networktype=getNetworkTypeString(tm.getNetworkType());
List cellinfo=tm.getNeighborringCellInfo();
如果(空!=cellinfo){
用于(邻居cellinfo:cellinfo){
((TextView)findViewById(R.id.neighbor)).setText(“CID:”+(info.getCid()&0xffff)+
“LAC:”+(info.getLac()&0xffff));
}
}
((TextView)findviewbyd(R.id.mnc)).setText(“mnc:+mnc”);
((TextView)findViewById(R.id.mcc)).setText(“mcc:+mcc”);
((TextView)findViewById(R.id.operatorname)).setText(“运算符:+运算符”);
//((TextView)findviewbyd(R.id.networkType)).setText(“网络类型:“+networkType”);
}
私有最终PhoneStateListener PhoneStateListener=新PhoneStateListener()
{
CellLocation更改后的公共void(CellLocation位置){
GsmCellLocation gsmLocation=(GsmCellLocation)位置;
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(“网络类型:”+(networkType)));
}
信号强度已更改(信号强度信号强度){
kualitas=signalStrength.getgsmbiterrorate();
kw3g=-1*(signalStrength.getgsmbiterrorate());
kuatlevel=-113+2*(signalStrength.getGsmSignalStrength());
如果(网络类型==“2G”){
setTextViewText(R.id.rxq_ecno,String.valueOf(“rxq:+(kualitas)));
setTextViewText(R.id.rxlrssi,String.valueOf(“rxl:”+(kuatlevel)+“dBm”);
}否则{
setTextViewText(R.id.rxq_ecno,String.valueOf(“ecno:”+(kw3g)+“dB”);
setTextViewText(R.id.rxlrssi,String.valueOf(“rssi:”+(kuatlevel)+“dBm”);
setTextViewText(R.id.arfcnu rscp,String.valueOf(“rscp:”+(kualtlevel+kw3g)+“dBm”);
}
}
};
私有字符串getNetworkTypeString(int-Ntype){
type=“未知”;
交换机(Ntype){
案例电话管理器。网络类型边缘:TYPE=“2G”中断;
案例电话管理器。网络类型\u GPRS:TYPE=“2G”中断;
案例电话管理器。网络类型UMTS:TYPE=“3G”中断;
case TelephonyManager.NETWORK_TYPE_1xRTT:TYPE=“2G”中断;
case TelephonyManager.NETWORK_TYPE_HSDPA:TYPE=“3G”中断;
违约:
type=“未知”中断;
}
//TODO自动生成的方法存根
返回类型;
}
受保护的void setTextViewText(int-id,字符串文本){
((TextView)findViewById(id)).setText(text);
//TODO自动生成的方法存根
}
//公共静态记录器getLogger(){
//返回记录器;
//}
}
使用PhoneStateListener.LISTEN\u SERVICE\u状态标志并覆盖ServiceStateChanged(ServiceState ServiceState),如下所示:

@Override
public void onServiceStateChanged(ServiceState serviceState){
    super.onServiceStateChanged(serviceState);
    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    int networkType = tm.getNetworkType();
}
此函数将在每次网络类型更改时启动。

使用PhoneStateListener.LISTEN\u SERVICE\u状态标志并覆盖onServiceStateChanged(ServiceState ServiceState),如下所示:

@Override
public void onServiceStateChanged(ServiceState serviceState){
    super.onServiceStateChanged(serviceState);
    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    int networkType = tm.getNetworkType();
}
此功能将在每次网络类型更改时启动