欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

完美解决android M上锁屏情况下,禁止pc通过MTP访问手机存储单元

程序员文章站 2022-05-07 11:40:19
1、问题解决主要文件:/m8976/packages/providers/mediaprovider/src/com/android/providers/media/mtp...

1、问题解决主要文件:/m8976/packages/providers/mediaprovider/src/com/android/providers/media/mtpservice.java

需要在mtpservice.java中updatedisabledstatelocked 方法添加锁屏情况限制:

final keyguardmanager keyguardmanager = (keyguardmanager) getsystemservice(
        context.keyguard_service);
mmtpdisabled = (keyguardmanager.iskeyguardlocked() && keyguardmanager.iskeyguardsecure()) || !munlocked || !iscurrentuser;

只要锁屏,mmtpdisabled就设置为true。

这样就实现该功能。

2、当解锁时,自动加载手机存储单元:

添加解锁监听

+  private final broadcastreceiver mreceiver = new broadcastreceiver() {
+    @override
+    public void onreceive(context context, intent intent) {
+      final string action = intent.getaction();
+      if (intent.action_user_present.equals(action)) {
+        // if the media scanner is running, it may currently be calling
+        // sendobjectadded/removed, which also synchronizes on mbinder
+        // (and in addition to that, all the native mtpserver methods
+        // lock the same mutex). if it happens to be in an mtp device
+        // write(), it may block for some time, so process this broadcast
+        // in a thread.
+        new thread(new runnable() {
+          @override
+          public void run() {
+            synchronized (mbinder) {
+              // unhide the storage units when the user has unlocked the lockscreen
+              if (mmtpdisabled) {
+                addstoragedeviceslocked();
+                mmtpdisabled = false;
+              }
+            }
+          }}, "addstoragedevices").start();
+      }
+    }
+  };

android m上发送解锁状态的广播code没有去除,可以查看/m8976/frameworks/base/packages/systemui/src/com/android /systemui/keyguard/keyguardviewmediator.java

在keyguardviewmediator.java文件中可以看出,在解锁时,发送intent.action_user_present广播,然后在mtpservice.java

进行接收,对mmtpdisabled进行处理。

以上这篇完美解决android m上锁屏情况下,禁止pc通过mtp访问手机存储单元就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。