Any alternative way to check if the network provider is turned on?

When i check

boolean networkReady=manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

I believe on some Samsung phones, although the location of the wireless network is not allowed in the settings.

Is there any other way to check this parameter and get the correct value for all phones?

+5
source share
3 answers

Below are some useful network features that I use in my applications, everything works like a charm! and for location, definitely → https://github.com/commonsguy/cwac-locpoll

hope this helps ...

public static boolean checkInternetConnection(Context context) {

    ConnectivityManager conMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    // ARE WE CONNECTED TO THE NET?
    if (conMgr.getActiveNetworkInfo() != null
            && conMgr.getActiveNetworkInfo().isAvailable()
            && conMgr.getActiveNetworkInfo().isConnected()) {
        return true;
    } else {
        Log.w(TAG, "Internet Connection NOT Present");
        return false;
    }
}
    public static boolean isConnAvailAndNotRoaming(Context context) {

    ConnectivityManager conMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (conMgr.getActiveNetworkInfo() != null
            && conMgr.getActiveNetworkInfo().isAvailable()
            && conMgr.getActiveNetworkInfo().isConnected()) {

        if(!conMgr.getActiveNetworkInfo().isRoaming())
            return true;
        else
            return false;
    } else {
        Log.w(TAG, "Internet Connection NOT Present");
        return false;
    }
}
    public static boolean isRoaming(Context context) {

    ConnectivityManager conMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    return (conMgr.getActiveNetworkInfo()!=null && conMgr.getActiveNetworkInfo().isRoaming());
}
+7
source

You can also try the following:

public boolean isDataConnectionAvailable(Context context){
        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if(info == null)
            return false;

        return connectivityManager.getActiveNetworkInfo().isConnected();
}

public boolean isGpsEnabled(LocationManager manager){
        if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            return false;
        }
        return true;
}

public boolean isLocationByNetworkEnabled(LocationManager manager){
        if ( !manager.isProviderEnabled( LocationManager.NETWORK_PROVIDER ) ) {
            return false;
        }
        return true;
}
+3

Is the provider in the list returned by getProviders (true); ? Perhaps this device believes that the network location provider should always be turned on to provide PASSIVE_PROVIDER? Seems to be broken to me. On which Samsung devices do you see this behavior?

0
source

All Articles