ScreenUtils Android屏幕工具类


import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.os.PowerManager;
import android.util.DisplayMetrics;
import android.view.View;

@SuppressWarnings("deprecation")
public class ScreenUtils {

    /**
     * 解锁屏幕
     *
     * @param context
     */
    public static void unlockScreen(Context context) {
        KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        KeyguardLock kl = km.newKeyguardLock("unLock");
        kl.disableKeyguard();

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
        wl.acquire(1000);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            wl.release();
        }
    }

    /**
     * 判断是否锁屏状态
     *
     * @param context
     * @return
     */
    public final static boolean isScreenLocked(Context context) {
        if (null != context) {
            android.app.KeyguardManager mKeyguardManager = (KeyguardManager) context.getSystemService(
                    Context.KEYGUARD_SERVICE);
            //* 如果flag为true,表示有两种状态:a、屏幕是黑的 b、目前正处于解锁状态 。
            //* 如果flag为false,表示目前未锁屏
            //* 关闭屏幕不等于锁屏
            return mKeyguardManager.inKeyguardRestrictedInputMode();
        }

        return false;
    }

    /**
     * 为true,则表示屏幕“亮”了,否则屏幕“暗”了。
     *
     * @param context
     * @return
     */
    public final static boolean isScreenOn(Context context) {
        if (null != context) {
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            return pm.isScreenOn();
        }

        return false;
    }

    /**
     * 屏幕屏幕密度
     *
     * @param context
     * @return
     */
    public static float getScreenDensity(Context context) {
        if (null != context) {
            return context.getResources().getDisplayMetrics().density;
        }
        return 0;
    }

    /**
     * 屏幕宽度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        if (null != context) {
            return context.getResources().getDisplayMetrics().widthPixels;
        }

        return 0;
    }

    /**
     * 屏幕高度
     */
    public static int getScreenHeight(Context context) {
        if (null != context) {
            return context.getResources().getDisplayMetrics().heightPixels;
        }

        return 0;
    }

    /**
     * 获取状态栏高度——方法4
     * 状态栏高度 = 屏幕高度 - 应用区高度
     * *注意*该方法不能在初始化的时候用
     * */
    public static int getStausBarHeight(Context context) {
        int statusBar=PixelUtil.dp2px(20, context);
        try{
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
                    "android");
            if (resourceId > 0) {
                statusBar = context.getResources().getDimensionPixelSize(resourceId);
            }
        }catch (Exception e){

        }
        return statusBar;
    }

你可能感兴趣的:(ScreenUtils Android屏幕工具类)