本文转载自:
Android L后默认的usb连接模式为“仅充电”,而且不会随用户每次的选择而记住,假如需要修改默认的usb连接模式并且需要记住用户的选择,需要修改以下一些代码:
1.需要把mk文件中的
persist.sys.usb.config=mtp
这样默认的连接模式就为MTP,但是修改此处后usb电脑手机重启后会在PC端显示两个手机储存】、
2.此时需要修改
packages/providers/MediaProvider/src/com/android/providers/media/MtpService.java public class MtpService extends Service {
public void onStorageStateChanged(String path, String oldState, String newState) {
synchronized (mBinder) {
Log.d(TAG, "onStorageStateChanged " + path + " " + oldState + " -> " + newState);
+ if (Environment.MEDIA_MOUNTED.equals(newState)
+ && Environment.MEDIA_MOUNTED.equals(oldState)
+ && mVolumeMap.containsKey(path)) {
+ Log.d(TAG, "avoid adding storage twice.");
+ return;
+ }
if (Environment.MEDIA_MOUNTED.equals(newState)) {
volumeMountedLocked(path);
} else if (Environment.MEDIA_MOUNTED.equals(oldState)) {
增加以上代码,就不会在PC端显示两个手机储存
3.修改以上两处后,每当用户开机第一次是mtp模式,但是连接一次后,第二次及以后连接都会是“仅充电”模式,此时还要修改
frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java
@Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_UPDATE_STATE: mConnected = (msg.arg1 == 1); mConfigured = (msg.arg2 == 1); if (!mConnected) { // When a disconnect occurs, relock access to sensitive user data mUsbDataUnlocked = false; } if (mConnected && (isUsbShouldActived())) { //setEnabledFunctions(null, false); } else if(mConnected && isLock) { setEnabledFunctions(mCurrentFunctions, false); updateAdbNotification(); } else { updateUsbNotification(); updateAdbNotification(); } if (UsbManager.containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_ACCESSORY)) { updateCurrentAccessory(); } else if ((!mConnected && !mCurrentFunctions.equals(UsbManager.USB_FUNCTION_NONE)) || mPowerDisconnected) { // restore defaults when USB is disconnected //setEnabledFunctions(null, false); }
注释掉红色部分的代码就可以了
#modify by tangzhihai 20161101 form "mass_storage" to "mtp" | 71 | |||
PRODUCT_DEFAULT_PROPERTY_OVERRIDES += \ | 72 | |||
» persist.sys.usb.config=mtp |