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:com.dmsl.anyplace.utils.NetworkUtils.java

public static boolean haveNetworkConnection(Activity activity) {

    boolean haveWifi = false;
    boolean haveMobile = false;

    ConnectivityManager cm = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni != null && ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnectedOrConnecting())
                haveWifi = true;/*  ww w.j a  va2s  .  c  om*/
        if (ni != null && ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnectedOrConnecting())
                haveMobile = true;
    }
    return haveMobile || haveWifi;
}

From source file:org.kontalk.util.SystemUtils.java

/**
 * Returns the correct screen orientation based on the supposedly preferred
 * position of the device.//from   w  ww .  ja  v  a 2 s.  c o m
 * http://stackoverflow.com/a/16585072/1045199
 */
public static int getScreenOrientation(Activity activity) {
    WindowManager windowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
    Configuration configuration = activity.getResources().getConfiguration();
    int rotation = windowManager.getDefaultDisplay().getRotation();

    // Search for the natural position of the device
    if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
            && (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180)
            || configuration.orientation == Configuration.ORIENTATION_PORTRAIT
                    && (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270)) {
        // Natural position is Landscape
        switch (rotation) {
        case Surface.ROTATION_0:
            return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        case Surface.ROTATION_90:
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
        case Surface.ROTATION_180:
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
        case Surface.ROTATION_270:
            return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        }
    } else {
        // Natural position is Portrait
        switch (rotation) {
        case Surface.ROTATION_0:
            return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        case Surface.ROTATION_90:
            return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        case Surface.ROTATION_180:
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
        case Surface.ROTATION_270:
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
        }
    }

    return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
}

From source file:library.artaris.cn.library.utils.SystemUtils.java

/**
 * ??/*from  w w w.jav a  2  s  .c o m*/
 * @param activity
 */
public static void hideKeyBoard(Activity activity) {
    ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
            activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

From source file:com.bt.download.android.gui.util.UIUtils.java

public static void hideKeyboardFromActivity(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity
            .getSystemService(Activity.INPUT_METHOD_SERVICE);
    View view = activity.getCurrentFocus();
    if (view == null) {
        view = new View(activity);
    }/*www .  j a v  a  2 s  . c o m*/
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

From source file:com.vinexs.tool.Utility.java

public static void showKeyBroad(Activity activity) {
    try {//from w ww.j a va 2s  .c  o  m
        InputMethodManager inputMethodManager = (InputMethodManager) activity
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(activity.getCurrentFocus(), InputMethodManager.SHOW_IMPLICIT);
    } catch (Exception ignored) {
    }
}

From source file:org.ttrssreader.utils.Utils.java

/**
 * Alert the user by a short vibration or a flash of the whole screen.
 *//*from  w w  w .  ja  v  a  2s. co  m*/
public static void alert(Activity activity, boolean error) {
    Vibrator vib = ((Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE));
    if (vib.hasVibrator()) {
        vib.vibrate(Utils.SHORT_VIBRATE);
    } else if (error) {
        // Only flash when user tried to move forward, flashing when reaching the last article looks just wrong.
        Animation flash = AnimationUtils.loadAnimation(activity, R.anim.flash);
        View main = activity.findViewById(R.id.frame_all);
        main.startAnimation(flash);
    }
}

From source file:de.baumann.hhsmoodle.helper.helper_main.java

public static void hideFilter(Activity activity, RelativeLayout layout, ImageView imageView) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(layout.getWindowToken(), 0);
    imageView.setVisibility(View.VISIBLE);
    layout.setVisibility(View.GONE);
}

From source file:eu.thecoder4.gpl.pleftdroid.PleftBroker.java

protected static boolean isInetConnAvailable(Activity a) {
    ConnectivityManager cm = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE);
    // test for connection
    if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable()
            && cm.getActiveNetworkInfo().isConnected()) {
        return true;
    } else {//from  ww  w  . jav a 2  s.c  om
        Log.i("check inet", "Internet Connection Not Present");
        return false;
    }
}

From source file:com.orange.ocara.ui.activity.BaseActivity.java

public static void hideSoftKeyboard(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity
            .getSystemService(Activity.INPUT_METHOD_SERVICE);
    View focusedView = activity.getCurrentFocus();
    if (focusedView != null) {
        inputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
    }//from ww w.  j  a  v a  2 s  .com

}

From source file:com.frostwire.android.gui.util.UIUtils.java

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