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 hideKeyboard(Context context) {
    Activity activity = (Activity) context;
    if (activity != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive() && activity.getCurrentFocus() != null) {
            imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
        }//from w  ww. ja v  a  2 s.  co m
    }
}

From source file:Main.java

public static String checkWifiConnection(final Activity activity) throws Exception {
    ConnectivityManager connManager = (ConnectivityManager) activity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (!mWifi.isConnected()) {
        new AlertDialog.Builder(activity).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Error")
                .setMessage("Wifi not connected. Application will exit.")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override//w  w  w  .  j ava2s .  c  o m
                    public void onClick(DialogInterface dialog, int which) {
                        //activity.finish();
                    }
                }).show();
        throw new Exception("Wifi");
    }
    WifiManager wifiMgr = (WifiManager) activity.getSystemService(Activity.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    return String.format("%d.%d.%d.%d", (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff), (ip >> 24 & 0xff));
}

From source file:Main.java

public static void hideSoftKeyboard(Activity activity) {
    if (activity.getCurrentFocus() != null) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity
                .getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }/*w  ww.j  av a 2s.c om*/
}

From source file:Main.java

public static void HideIme(Activity activity) {
    if (activity.getCurrentFocus() != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    }//w  w w.  jav a 2 s  . co m
}

From source file:Main.java

public static void hideIME(Activity context, boolean bHide) {
    if (bHide) {//from   w  ww .  j  a v  a 2  s  .  c o m
        try {
            ((InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (NullPointerException npe) {
            npe.printStackTrace();
        }
    } else { // show IME
        try {
            ((InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE))
                    .showSoftInput(context.getCurrentFocus(), InputMethodManager.SHOW_IMPLICIT);
        } catch (NullPointerException npe) {
            npe.printStackTrace();
        }
    }
}

From source file:Main.java

/**
 * Close the soft key board//w w w.j  a  v  a  2 s .  c  o m
 */
public static void closeSoftKeyBoard(Activity pActivity) {
    if (pActivity != null) {
        final InputMethodManager inputMethodManager = (InputMethodManager) pActivity
                .getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null) {
            final View currentFocus = pActivity.getCurrentFocus();
            if (currentFocus != null) {
                inputMethodManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
            }

        }
    }
}

From source file:Main.java

/**
 * get the SoftInput state/*from   w w w .j  a  va2  s.  com*/
 * 
 * @param activity
 * @return if shown return true else return false
 */
public static boolean softInputIsShow(Activity activity) {
    if (activity == null) {
        return false;
    }
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    return imm.isActive();
}

From source file:Main.java

public static boolean hideSoftInput(Activity activity) {
    if (activity.getCurrentFocus() != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        return imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }/* w ww . j a v  a 2  s .com*/
    return false;
}

From source file:Main.java

public static void showSoftKeyborad(Activity context, EditText et) {
    if (context == null)
        return;//from  w  ww  . j a  v a 2 s .  c  o  m
    InputMethodManager inputMethodManager = (InputMethodManager) context
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.showSoftInput(et, InputMethodManager.SHOW_FORCED);
}

From source file:Main.java

public static void hide(Activity activity) {
    View view = activity.getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }/*ww w .j a  v a  2  s .c om*/
}