Android开发

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

本文主要是介绍完美解决android M上锁屏情况下,禁止pc通过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访问手机存储单元就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持找一找教程网。

这篇关于完美解决android M上锁屏情况下,禁止pc通过MTP访问手机存储单元的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!