Code Snippets - Android

Toast

A "toast" is a small popup near the bottom of the screen.

Toast.makeText(getApplicationContext(), "Hello, world!", Toast.LENGTH_SHORT).show();

Network Connectivity

private boolean isWifiConn;
private boolean isMobilConn;

public boolean checkForNetwork()
{
    ConnectivityManager connMan = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo wifiConn = connMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mobileConn = connMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    isWifiConn = (wifiConn != null) && (wifiConn.isConnected());        	
    isMobilConn = (mobileConn != null) && (mobileConn.isConnected());

    return isWifiConn || isMobilConn;
}