Reputation: 279
I have implemented network type fetch in my project but in Android 10 its not working, I have tried to find out solution but not getting success. Here is my code but it always return blank (Unknown type)
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static String getNetworkClass(Context context) {
TelephonyManager mTelephonyManager = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = mTelephonyManager.getNetworkType();
printLog("networkType","-"+networkType);
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: {
showToast(context, "Connection Available is 2G");
return "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: {
showToast(context, "Connection Available is 3G");
return "3G";
}
case TelephonyManager.NETWORK_TYPE_LTE: {
showToast(context, "Connection Available is 4G");
return "4G";
}
case TelephonyManager.NETWORK_TYPE_NR:
{
showToast(context, "Connection Available is 5G");
return "5G";
}
default: {
showToast(context, "Not detect");
return "";
}
}
Upvotes: 0
Views: 1386
Reputation: 279
To get network type in android 10 or above used mTelephonyManager.getDataNetworkType(); instated of mTelephonyManager.getNetworkType();
here is working function :
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static String getNetworkClass(Context context) {
TelephonyManager mTelephonyManager = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = mTelephonyManager.getNetworkType();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
//return TODO;
networkType = mTelephonyManager.getDataNetworkType();
}
} else {
networkType = mTelephonyManager.getNetworkType();
}
printLog("networkType", "-" + networkType);
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: {
showToast(context, "Connection Available is 2G");
return "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: {
showToast(context, "Connection Available is 3G");
return "3G";
}
case TelephonyManager.NETWORK_TYPE_LTE: {
showToast(context, "Connection Available is 4G");
return "4G";
}
case TelephonyManager.NETWORK_TYPE_NR: {
showToast(context, "Connection Available is 5G");
return "5G";
}
default: {
showToast(context, "Not detect");
return "";
}
}
}
Upvotes: 3