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 void hideSoftInput(Activity activity) {
    try {/* w ww .j  a  v  a 2 s  . c o m*/
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void showSoftInput(Activity activity) {
    try {/*from  www.j av  a2  s.c  om*/
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static boolean hasInternetAccess(Activity activity) {
    ConnectivityManager cm = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm.getActiveNetworkInfo() == null)
        return false;
    boolean ret = cm.getActiveNetworkInfo().isConnectedOrConnecting();
    return ret;//from  w w  w. j  a va  2  s  . co m

}

From source file:Main.java

/**
 * Hide keyboard//  w w  w  . ja v  a2  s  . co m
 * 
 * @param activity
 */
public static void hideKeyboard(Activity activity) {

    InputMethodManager inputMethodManager = (InputMethodManager) activity
            .getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

From source file:Main.java

public static void hideSoftKeyboard(Activity activity) {

    InputMethodManager inputMethodManager = (InputMethodManager) activity
            .getSystemService(Context.INPUT_METHOD_SERVICE);

    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
            InputMethodManager.HIDE_IMPLICIT_ONLY);
}

From source file:Main.java

public static void lockOrientation(Activity activity) {
    Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int rotation = display.getRotation();
    int tempOrientation = activity.getResources().getConfiguration().orientation;
    int orientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
    switch (tempOrientation) {
    case Configuration.ORIENTATION_LANDSCAPE:
        if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        else/*from w w w  .  j  a  v a 2s  .  c  o  m*/
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270)
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        else
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
    }

    activity.setRequestedOrientation(orientation);
}

From source file:Main.java

public static void copy2Clipboard(Activity activity, String content) {
    ClipboardManager clipboardManager = (ClipboardManager) activity
            .getSystemService(Activity.CLIPBOARD_SERVICE);
    clipboardManager.setText(content);/*from  w  w  w .  j  a  v  a2  s  .c o  m*/
}

From source file:Main.java

/**
 * This method is used to hide a keyboard after a user has
 * finished typing the input.//from  w  ww  .j a  va  2 s  .  c om
 */
public static void hideKeyboard(Activity activity, IBinder windowToken) {
    InputMethodManager mgr = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(windowToken, 0);
}

From source file:Main.java

public static void showInputMethod(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    View currentFocus = activity.getCurrentFocus();
    if (currentFocus != null) {
        imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        imm.showSoftInput(currentFocus, InputMethodManager.SHOW_IMPLICIT);
    }//from ww w  .ja  v a 2s .  c om

    //        if (imm != null) {
    //            imm.viewClicked(this);
    //        }
    //        if (imm != null && getShowSoftInputOnFocus()) {
    //            imm.showSoftInput(this, 0);
    //        }
}

From source file:Main.java

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

    // check if no view has focus:
    View v = activity.getCurrentFocus();
    if (v == null)
        return;/* w  w w .java  2s  . com*/

    inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}