Android后台监控其他应用是否全屏

网上有很多判断是否全屏的方法,例如:判断当前屏幕是否是全屏+是否是竖屏

 /**
 * @param activity
 * @return 判断当前手机是否是全屏
 */
public static boolean isFullScreen(Activity activity) {
    int flag = activity.getWindow().getAttributes().flags;  
    if((flag & WindowManager.LayoutParams.FLAG_FULLSCREEN) 
            == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
        return true;
    }else {
        return false;
    }
}

设置全屏的方法2种

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
              WindowManager.LayoutParams. FLAG_FULLSCREEN);//全屏

取消全屏的方法

getWindow().clearFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN); 

/**
 * 判断当前屏幕是否是横屏
 * @param activity
 * @return
 */
public static boolean isVerticalScreen(Activity activity) {
    int flag = activity.getResources().getConfiguration().orientation;
    if (flag == 0) {
        return false;
    }else {
        return true;
    }
}

这个方法只能判断当前应用的activity是否全屏,如果我们需要在后台监控其他的应用是否全屏,显然上面这些方法是不起作用的。

今天刚好看到了一种实现方法:【Android】Service中判断当前是否全屏(状态栏是否隐藏)

大致原理:创建了一个overlay的不可见View,靠在窗口上方,通过调用View.getLocationOnScreen判断其中Y轴位置的值,来判断是否全屏

另外也大致实现了一下,确实可行,所以mark一下

public class MyService extends Service {
    private View mView;
    private static final String TAG = "MyService";
    public MyService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mView = new Button(this);
        mView.setBackgroundColor(Color.parseColor("#ff0dd1"));

        WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE; 
        wmParams.flags= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        wmParams.gravity = Gravity.TOP| Gravity.LEFT; 
        wmParams.x = 0;
        wmParams.y = 0;
        wmParams.width = 100;
        wmParams.height = 100;
        try {
            windowManager.addView(mView, wmParams);
        } catch (Exception e) {
            e.printStackTrace();
        }

        new Thread(new Runnable() {
            @Override
            public void run() { 
                    int[] top = new int[2];
                    mView.getLocationOnScreen(top); 
                    if (top[1] == 0) {
                     Log.d(TAG, "Y " + top[1] + "current is full screen");
                    } else {
                    Log.d(TAG, "Y " + top[1] + "current is not full screen");
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();

        return super.onStartCommand(intent, flags, startId);
    }
}

欢迎关注微信公众号:DroidMind
精品内容独家发布平台
Android后台监控其他应用是否全屏_第1张图片

呈现与博客不一样的技术干货

你可能感兴趣的:(Android开发)