2014-09-10 49 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

相關問題