2014-09-10 64 views
0

我想在烤面包中显示哪个网络连接可用。 但是当我开始我的应用程序时,它显示了我的布局,但它并没有烤面包。 我忘了什么吗?在烤面包中显示网络连接2g/3g或4g

//现在我每秒钟都会显示吐司。它开始与正确的一个,但然后它去下一个.. 我现在也有一个按钮,它显示了在textview中的networktyp。但它总是只显示4g .. 在此先感谢您的帮助!

Button start; 
TextView ergebniss; 

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

    start = (Button)findViewById(R.id.start); 
    start.setOnClickListener(this); 

    ergebniss = (TextView) findViewById(R.id.textView1); 

} 


public void getNetworkClass(Context context) { 
    TelephonyManager mTelephonyManager = (TelephonyManager) context 
      .getSystemService(Context.TELEPHONY_SERVICE); 
    int networkType = mTelephonyManager.getNetworkType(); 
    switch (networkType) { 

    case TelephonyManager.NETWORK_TYPE_GPRS: 
    case TelephonyManager.NETWORK_TYPE_EDGE: 
    case TelephonyManager.NETWORK_TYPE_CDMA: 
    case TelephonyManager.NETWORK_TYPE_1xRTT: 
    case TelephonyManager.NETWORK_TYPE_IDEN: 
    Toast.makeText(getApplicationContext(), "2G", Toast.LENGTH_LONG).show(); 
     ergebniss.setText("2G"); 


    case TelephonyManager.NETWORK_TYPE_UMTS: 
    case TelephonyManager.NETWORK_TYPE_EVDO_0: 
    case TelephonyManager.NETWORK_TYPE_EVDO_A: 
    case TelephonyManager.NETWORK_TYPE_HSDPA: 
    case TelephonyManager.NETWORK_TYPE_HSUPA: 
    case TelephonyManager.NETWORK_TYPE_HSPA: 
    case TelephonyManager.NETWORK_TYPE_EVDO_B: 
    case TelephonyManager.NETWORK_TYPE_EHRPD: 
    case TelephonyManager.NETWORK_TYPE_HSPAP: 
     Toast.makeText(getApplicationContext(), "3G", Toast.LENGTH_LONG).show(); 
     ergebniss.setText("3G"); 


    case TelephonyManager.NETWORK_TYPE_LTE: 
     Toast.makeText(getApplicationContext(), "4G", Toast.LENGTH_LONG).show(); 
     ergebniss.setText("4G"); 


    } 

} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    getNetworkClass(this); 
} 

}

回答

0

您需要至少显示吐司。

Toast.makeText(getApplicationContext(), "2G", Toast.LENGTH_LONG).show(); 

而你缺少break;(或返回..)的Toast.makeText()来电之后的语句。

+0

我确实改变了代码。但它仍然不起作用。 – user3379235 2014-09-10 14:03:35

+0

然后,您何时/何时调用getNetworkClass()方法? – nos 2014-09-10 16:41:48