Example usage for android.app Activity getSystemService

List of usage examples for android.app Activity getSystemService

Introduction

In this page you can find the example usage for android.app Activity getSystemService.

Prototype

@Override
    public Object getSystemService(@ServiceName @NonNull String name) 

Source Link

Usage

From source file:Main.java

public static boolean checkInternet(Activity act) {
    ConnectivityManager conMgr = (ConnectivityManager) act.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo i = conMgr.getActiveNetworkInfo();
    if (i == null) {
        return false;
    }//from   w w  w .j  av a  2 s .co m
    if (!i.isConnected()) {
        return false;
    }
    if (!i.isAvailable()) {
        return false;
    }
    return true;
}

From source file:Main.java

public static boolean isNetworkAvailable(Activity activity) {
    ConnectivityManager connectivity = (ConnectivityManager) activity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        return false;
    } else {/*from   w  w w . ja  v a2 s .  c om*/
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:Main.java

public static void showKeyBoard(Activity activity) {
    if (activity != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }//from   www. j a v  a 2 s . c o m
}

From source file:Main.java

public static void closeInputMethod(final Activity activity, final View view) {
    final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

From source file:Main.java

public static boolean hasInternet(Activity activity) {

    ConnectivityManager manager = (ConnectivityManager) activity

            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo info = manager.getActiveNetworkInfo();

    if (info == null || !info.isConnected()) {

        return false;
    }/*from www  . j  av  a2  s .  c om*/
    if (info.isRoaming()) {
        // here is the roaming option you can change it if you want to

        // disable internet while roaming, just return false

        return true;
    }
    return true;
}

From source file:Main.java

/**
 * Hides the soft keyboard//from   w w w . ja v a 2  s . co  m
 *
 * @param activity The reference of the activity displaying the keyboard
 */
public static void hide(@NonNull final Activity activity) {
    final InputMethodManager iManager = (InputMethodManager) activity
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    final View view = activity.getCurrentFocus();
    if (view != null && iManager != null) {
        iManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

From source file:Main.java

public static void closeInputMethodWindow(Activity activity) {
    try {//from  w ww.j  a  v a 2s  .  co  m
        InputMethodManager inputMethodManager = (InputMethodManager) activity
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    } catch (Throwable e) {
    }
}

From source file:Main.java

public static void hideKeyboard(android.app.Activity _activity) {
    InputMethodManager inputManager = (InputMethodManager) _activity
            .getSystemService(Context.INPUT_METHOD_SERVICE);

    if (_activity.getCurrentFocus() != null) {
        inputManager.hideSoftInputFromWindow(_activity.getCurrentFocus().getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    } else {/*from   www. jav a2  s. co  m*/
        inputManager.hideSoftInputFromWindow(null, InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

From source file:Main.java

public static void hideKeyboard(Activity _activity, View _view) {
    InputMethodManager inputMethodManager = (InputMethodManager) _activity
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(_view.getWindowToken(), 0);
}

From source file:Main.java

public static void showKeyboard(Activity activity) {
    if (activity != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (!imm.isActive()) {
            imm.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
        }//from ww  w  . j a v  a 2  s  . c  o  m
    }
}