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 closeInputMethod(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    if (imm != null) {
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
        }/*w  w w  . j  a  va  2  s  .c  o m*/
    }
}

From source file:Main.java

public static void hidekeyboard(Activity act) {
    InputMethodManager imm = ((InputMethodManager) act.getSystemService(Activity.INPUT_METHOD_SERVICE));
    if (imm.isActive()) {
        View focusView = act.getCurrentFocus();
        if (focusView != null) {
            imm.hideSoftInputFromWindow(focusView.getWindowToken(), 0);
        }/*www  .  j  av  a 2s .c om*/
    }
}

From source file:Main.java

public static void showKeyboard(Activity activity, View v) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
}

From source file:Main.java

public static void hideInputMethodManager(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    View v = activity.getCurrentFocus();
    if (imm != null && v != null) {
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }/*from w w  w  . ja v a 2  s.  c  o  m*/
}

From source file:Main.java

public static double[] getLastKnownLocation(Activity a) {
    LocationManager locationManager = (LocationManager) a.getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    System.out.println("provider: " + provider);
    Location location = locationManager.getLastKnownLocation(provider);
    return new double[] { location.getLatitude(), location.getLongitude() };
}

From source file:Main.java

public static String getTopActivity(Activity context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
    List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);

    if (runningTaskInfos != null)
        return (runningTaskInfos.get(0).topActivity).getShortClassName();
    else//ww w . j  a v a2 s . c om
        return null;
}

From source file:Main.java

private static boolean isTelephonyEnabled(Activity activity) {
    TelephonyManager telephonyManager = (TelephonyManager) activity
            .getSystemService(Activity.TELEPHONY_SERVICE);
    return telephonyManager != null && telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY;
}

From source file:Main.java

public static void Vibrate(final Activity activity, long milliseconds) {
    Vibrator vib = (Vibrator) activity.getSystemService(Service.VIBRATOR_SERVICE);
    vib.vibrate(milliseconds);/*from ww  w . j av  a  2 s . c om*/
}

From source file:Main.java

public static void hideSoftKeyboard(Activity a) {
    InputMethodManager inputMethodManager = (InputMethodManager) a
            .getSystemService(Activity.INPUT_METHOD_SERVICE);
    if (a.getCurrentFocus() != null && a.getCurrentFocus().getWindowToken() != null)
        inputMethodManager.hideSoftInputFromWindow(a.getCurrentFocus().getWindowToken(), 0);
}

From source file:Main.java

public static void Vibrate(final Activity activity, long milliseconds) {
    Vibrator vibrator = (Vibrator) activity.getSystemService(Service.VIBRATOR_SERVICE);
    vibrator.vibrate(milliseconds);/*ww w.  j av  a 2 s.c  o  m*/
}