由于机器可能是横屏机器或者竖屏机器,并且因为功能需要不支持旋转屏幕。此时如果在机器上安装第三方APK,如微信,需要默认为和机器一样的方向,然而没有第三方APK的源码,修改默认屏幕方向的话只能需要去系统里想办法啦。
系统中修改APK默认方向的话,在WindowManagerService .java这个类中可以修改。
首先需要知道当前应用的包名:获取应用包名,可以通过adb命令获取;
adb shell pm list packages -3 获取当前安装的所有第三方APK的包名
aapt dump badging [app.apk] 获取APK的信息
知道了APK的包名之后,就可以在通过包名去过滤,控制方向旋转了。
1.获取当前进程名
在WindowManagerService .java这个类中添加如下方法获取当前正在运行的前台进程名
+ private boolean isWeChat = false;
+
+ private String getCurProcessName(Context context) {
+ final int PROCESS_STATE_TOP = 2;
+ try {
+ Field processStateField = ActivityManager.RunningAppProcessInfo.class.getDeclaredField("processState");
+ List<ActivityManager.RunningAppProcessInfo> processes = ((ActivityManager) context.getSystemService(
+ Context.ACTIVITY_SERVICE)).getRunningAppProcesses();
+ for (ActivityManager.RunningAppProcessInfo process : processes) {
+ if (process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
+ int state = processStateField.getInt(process);
+ if (state == PROCESS_STATE_TOP) {
+ String[] packname = process.pkgList;
+ return packname[0];
+ }
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return "";
+ }
2.判断是否是微信
在如下位置添加当前代码:判断是否是微信
return null;
}
Configuration config = null;
+ if("com.tencent.mm".equals(getCurProcessName(mContext))){
+ isWeChat = true;
+ }else{
+ isWeChat = false;
+ }
if (updateOrientationFromAppTokensLocked(false, displayId)) {
// If we changed the orientation but mOrientationChangeComplete is already true,
// we used seamless rotation, and we don't need to freeze the screen.
3.控制旋转方向
long ident = Binder.clearCallingIdentity();
try {
final DisplayContent dc = mRoot.getDisplayContent(displayId);
- final int req = dc.getOrientation();
- if (req != dc.getLastOrientation()) {
- dc.setLastOrientation(req);
- //send a message to Policy indicating orientation change to take
- //action like disabling/enabling sensors etc.,
- // TODO(multi-display): Implement policy for secondary displays.
- if (dc.isDefaultDisplay) {
- mPolicy.setCurrentOrientationLw(req);
- }
- if (dc.updateRotationUnchecked(inTransaction)) {
- // changed
- return true;
- }
- }
+ int req = dc.getOrientation();
+ if(isWeChat){
+ /*如果是微信则不旋转*/
+ }else{
+ if (req != dc.getLastOrientation()) {
+ dc.setLastOrientation(req);
+ //send a message to Policy indicating orientation change to take
+ //action like disabling/enabling sensors etc.,
+ // TODO(multi-display): Implement policy for secondary displays.
+ if (dc.isDefaultDisplay) {
+ mPolicy.setCurrentOrientationLw(req);
+ }
+ if (dc.updateRotationUnchecked(inTransaction)) {
+ // changed
+ return true;
+ }
+ }
+ }
以微信为例,可以在此处对第三方APK的方向进行操作。