Friday 10 February 2017

Network provider checksum for android (phone/tablet)

It is important to check network availability before starting search for network services like jmdns search, network check before doing any kind of network operations. 

 

1) Util class for network check

 

/**
 * This class checks network state in mobile device
 */
public class NetworkUtil {

    /**
     * The refrence.
     */
    public static NetworkUtil refrence = null;

    /**
     * Method returns class object.
     *
     * @return single instance of NetworkUtil
     */
    public static NetworkUtil getInstance() {
        if (null == refrence)
            refrence = new NetworkUtil();
        return refrence;
    }


    /**
     * Check network state.
     *
     * @param context the context
     * @return true, if successful
     */
    public static boolean checkNetworkStatus(Context context) {
        boolean hasConnectedWifi = false;
        boolean hasConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();

        //For loop to get all data provide in mobile device
        for (NetworkInfo ni : netInfo) {
            //If wifi detected then check for connection state
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected()){
                    hasConnectedWifi = true;
                }


            //If mobile data detected then check for connection state
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    hasConnectedMobile = true;
        }
        return hasConnectedWifi || hasConnectedMobile;
    }
}

 

Here is the code to access NetworkUtil class where you have to pass context of activity and will return true/false to show network availability.

 

boolean isNetworkAvailable=

    NetwrokUtil.checkNetworkStatus(context);

 

Here we done with Network check in android device (phone/tablet).

 

Thanks...!!!

 

Andorid marshmallow custom permission code

As we all know android has put some restrictions for permission check for security reasons from android M (6.0) Marshmallow. Here we gon...