Вопрос

Is there a way to check if the user is using a device (this applies primarily to tablets) with Cellular conection?. That is, the smartphones comes with built-in Wi‑Fi and Cellular (generally), but some tablets only comes with Wi-Fi. How I can know what type of device is running my application?

I tried the following without results:

cell = ConnectivityManager.isNetworkTypeValid(ConnectivityManager.TYPE_MOBILE);
wifi = ConnectivityManager.isNetworkTypeValid(ConnectivityManager.TYPE_WIFI);

if (cell) tv_1.setText("The tablet has cellular");
   else tv_1.setText("The tablet does not have cellular");
if (wifi) tv_2.setText("The tablet has wifi");
   else tv_2.setText("The tablet does not have wifi");

The problem is that both comparisons always return true, even if it's a tablet that has no cellular.

I only need to know if the device has a SIM card slot (model with cellular) or it is a model that only has WiFi, is that possible?

Thanks in advance.

Это было полезно?

Решение 2

Here is excerpt from my code (it works so far):

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mEthernet = connManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
NetworkInfo m3G = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mWifi!=null) isOnWifi = mWifi.isConnected();
if (mEthernet!=null) isOnEthernet = mEthernet.isConnected();
if (m3G!=null) is3G = m3G.isConnected();

Другие советы

If the goal is to determine whether the connection is metered, you should call ConnectivityManager.isActiveNetworkMetered(), or if older device support is required, ConnectivityManagerCompat.isActiveNetworkMetered().

For background on reacting to varying connection types, see the Managing Network Usage (although be aware of that documentation's problem of not using isActiveNetworkMetered()).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top