In these days, I've published two updated android application to Google Play (Planet Myanmar News and Planet Myanmar Dictionary). In both of them, I've changed 100% sidemenu UI design like Facebook and GooglePlus for easy to use and navigate. You may ask why I didn't use Tabs and Menu features of android. It's because of I don't want those tabs will overlap on my screen and want bigger screen for my application. For menu, I don't want to use because some android users don't know how to use menu (I mean, which button needed to click to appear menu action).

Let's start my topic. In these two updated applications, I've added Sync feature to update information when device is connected by wifi or mobile network. At that time, I need to check whether device is connected by wifi or mobile network. If not connected, I need to alert message to user to connect if he/she want to sync.

Thus I need to write below function first. This function will return "true" when device is connected by wifi or mobile network.

view plain print about
1public static Boolean isNetAvailable(Context con) {        
2 try{
3 ConnectivityManager connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
4 NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
5 NetworkInfo mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
6 if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
7 return true;
8 }
9 }
10 catch(Exception e){
11 e.printStackTrace();
12 }
13 return false;
14}

How to use is very simple.

view plain print about
1if (common.isNetAvailable(this) == false) {
2    Toast toast = Toast.makeText(Sync.this, "Please turn on mobile or wifi network to sync", Toast.LENGTH_SHORT);
3    toast.setGravity(Gravity.CENTER, 0, 0);
4    toast.show();
5}