I would like to display in a toast which network connection is available. But when I start my app it shows me the layout, but it doesn't make a toast. Did I forget something?
// Now i shows every second a toast. it beginns with the right one but then it goes to the next one.. I also have a button now, which shows me the networktyp in a textview. But it always just show the 4g.. Thanks in advance for your help!
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);
}
}