修改APP默认横屏或竖屏

由于机器可能是横屏机器或者竖屏机器,并且因为功能需要不支持旋转屏幕。此时如果在机器上安装第三方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的方向进行操作。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值