//屏幕方向监听
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);
}