AndroidScreenRotateUtils.class
import android.os.IBinder;
import android.os.ServiceManager;
import android.view.IRotationWatcher;
import android.view.IWindowManager;
import android.view.View;
public class AndroidScreenRotateUtils {
private static IWindowManager iWindowManager;
private static AndroidScreenRotateUtils instance;
public volatile static int rotation = 0;
public static class RotationWatcherImpl extends IRotationWatcher.Stub {
@Override
public void onRotationChanged(int arg3) {
int newRatation = (arg3 & 0x1);
MyLog.d("onRotationChanged:" + newRatation);
if (rotation != newRatation) {
rotation = newRatation;
}
}
}
public void init() {
IBinder v0 = ServiceManager.getService("window");
if (v0 != null) {
iWindowManager = IWindowManager.Stub.asInterface(v0);
try {
iWindowManager.watchRotation(new RotationWatcherImpl(), 0);
rotation = iWindowManager.getDefaultDisplayRotation();
} catch (Exception v0_1) {
v0_1.printStackTrace();
}
}
}
public static AndroidScreenRotateUtils getInstance() {
if (instance == null) {
synchronized (AndroidScreenRotateUtils.class) {
if (instance == null) {
instance = new AndroidScreenRotateUtils();
}
return instance;
}
}
return instance;
}
}
1、需要 IRotationWatcher.aidl
package android.view;
interface IRotationWatcher {
void onRotationChanged(int rotation);
}
2、需要 IWindowManager.aidl
public interface IWindowManager {
@RequiresApi(26)
int watchRotation(IRotationWatcher watcher, int displayId);
@RequiresApi(26)
int getDefaultDisplayRotation();
}