Android高級 后臺獲取屏幕方向

        //屏幕方向監(jiān)聽
        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);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容