Android高级 后台获取屏幕方向

        //屏幕方向监听
        ScreenRotateUtils.getInstance().setiRotationChanged(new ScreenRotateUtils.IRotationChanged() {
            @Override
            public void onRotationChanged(int rotation) {
                MyLog.e("rotation:" + rotation);
                MyConfig.getInstance().rotation = rotation;
                if (rotation == 0) {
                    
                } else {
                     
                }
            }
        });

ScreenRotateUtils.class


package com.shanwan.common.utils;

import android.os.Build;
import android.os.IBinder;
import android.view.IRotationWatcher;
import org.lsposed.hiddenapibypass.HiddenApiBypass;
import java.lang.reflect.Method;

public class ScreenRotateUtils {
    private static ScreenRotateUtils instance;
    private int rotation = 0;
    private IRotationChanged iRotationChanged;

    public interface IRotationChanged {
        void onRotationChanged(int rotation);
    }

    public static Class<?>[] getMethodParamTypes(Class<?> clazz, String methodName) {
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            if (methodName.equals(method.getName())) {
                return method.getParameterTypes();
            }
        }
        return null;
    }


    public void setiRotationChanged(IRotationChanged iRotationChanged) {
        this.iRotationChanged = iRotationChanged;
        try {
            Method getServiceMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", new Class[]{String.class});
            Object ServiceManager = getServiceMethod.invoke(null, new Object[]{"window"});
            Class<?> cStub = Class.forName("android.view.IWindowManager$Stub");
            Method asInterface = cStub.getMethod("asInterface", IBinder.class);
            Object iWindowManager = asInterface.invoke(null, ServiceManager);
            Class<?>[] paramTypes = getMethodParamTypes(iWindowManager.getClass(),"watchRotation");
            Method watchRotation = iWindowManager.getClass().getDeclaredMethod("watchRotation",paramTypes);
//            Method watchRotation = iWindowManager.getClass().getMethod("watchRotation", IRotationWatcher.class);
//            watchRotation.invoke(iWindowManager, iRotationWatcher);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//这里做个适配
                rotation = (int) watchRotation.invoke(iWindowManager, iRotationWatcher, 0);
            } else {
                rotation = (int) watchRotation.invoke(iWindowManager, iRotationWatcher);
            }
        } catch (Exception e) {
            e.printStackTrace();
            MyLog.e("e:"+e);
        }
//        iRotationChanged.onRotationChanged(rotation);
    }

    private final IRotationWatcher iRotationWatcher = new IRotationWatcher.Stub(){

        private int newRatation;

        @Override
        public void onRotationChanged(int rotation) {
            //do sth
            MyLog.e("rotation:"+rotation);
            if (rotation != newRatation) {
                newRatation = rotation;
                iRotationChanged.onRotationChanged(rotation);
            }
        }
    };

    private ScreenRotateUtils() {
        if (Build.VERSION.SDK_INT >= 28)
            HiddenApiBypass.setHiddenApiExemptions("L");
    }



    public static ScreenRotateUtils getInstance() {
        if (instance == null) {
            synchronized (ScreenRotateUtils.class) {
                if (instance == null) {
                    instance = new ScreenRotateUtils();
                }
                return instance;
            }
        }
        return instance;
    }
}

IRotationWatcher.aidl

// IRotationWatcher.aidl
package android.view;

// Declare any non-default types here with import statements

interface IRotationWatcher {
 void onRotationChanged(int rotation);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容