开发者

How do I check if a user has enabled a network regardless of whether or not that network is connected?

开发者 https://www.devze.com 2023-04-07 14:26 出处:网络
All I want to do is see if the network is actually enabled (whether or not there is a check bythe option in the settings menu). How do I go about doi开发者_StackOverflow社区ng this?

All I want to do is see if the network is actually enabled (whether or not there is a check by the option in the settings menu). How do I go about doi开发者_StackOverflow社区ng this?

To be specific:

I got Wifi through WifiManager... ...and everything else has been a nightmare with everything else (specifically mobile data).

This is how I'm trying with Mobile Data and WiMax:

cm.getNetworkInfo(cm.TYPE_WIMAX).getDetailedState();
cm.getNetworkInfo(cm.TYPE_MOBILE).getDetailedState();

But those return DISCONNECTED when they are disabled or actually disconnected.

EDIT: I found it: http://developer.android.com/reference/android/provider/Settings.System.html

EDIT again: This doesn't really have what I want either... I will keep searching, though!


NetworkInfo info = connectivityManager.getActiveNetworkInfo();

boolean isNetworkEnabled = (info == null ? false : true);


Were you thinking of implementing these two methods from LocationListener: onProviderEnabled(), onProviderDisabled()?

However, I think requestLocationUpdates() must be called first before with the listener before these methods will be called.


    public static boolean checkConnection(Context context) {
            ConnectivityManager connMgr = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (wifi.isAvailable() && wifi.isConnected()) {
                status = true;
            }
            if (mobile.isAvailable() && mobile.isConnected()) {
                status = true;
            } else {
                status = false;
            }
return status;
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消