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就設(shè)置為true。
這樣就實現(xiàn)該功能。
2、當(dāng)解鎖時,自動加載手機存儲單元:
添加解鎖監(jiān)聽
+ 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上發(fā)送解鎖狀態(tài)的廣播code沒有去除,可以查看/m8976/frameworks/base/packages/SystemUI/src/com/android /systemui/keyguard/KeyguardViewMediator.java
在KeyguardViewMediator.java文件中可以看出,在解鎖時,發(fā)送Intent.ACTION_USER_PRESENT廣播,然后在MtpService.java
進行接收,對mMtpDisabled進行處理。
以上這篇完美解決android M上鎖屏情況下,禁止pc通過MTP訪問手機存儲單元就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持武林網(wǎng)。
新聞熱點
疑難解答