6.0之前
/device/mediatek/common/device.mk
# default usb function ifeq ($(strip $(MTK_MASS_STORAGE)),yes)
ADDITIONAL_DEFAULT_PROPERTIES += persist.sys.usb.config=mass_storage
//修改上面成想要的功能,如 ADDITIONAL_DEFAULT_PROPERTIES += persist.sys.usb.config=mtp
6.0之后
MTK 在M版本将沿用Google的设计
由于改动之后会有些副作用,
所以MTK并不支持修改
没办法
客户需要
根据弹出选择USB 模式的界面可以看到 最终是通过UsbManager 进行实现的,
那么我们可以在USB查吧的时候模拟点击的是MTP
有了思路,那么进行改动
mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MTP);
mUsbManager.setUsbDataUnlocked(true);
既然会发送通知,那么肯定会有接受USB插拔的广播
StorageNotification.java
/*chc add */
private UsbManager mUsbManager;
int i=0;
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean available = (intent.getBooleanExtra(UsbManager.USB_CONFIGURED, false) &&
intent.getBooleanExtra(UsbManager.USB_FUNCTION_MASS_STORAGE, false));
Log.i(TAG, "onReceive=" + intent.getAction() + ",available=" + available);
/*chc add */
boolean connected = intent.getExtras().getBoolean("connected"); //进行检测USB是连接还是断开的
Log.d(TAG, "connected="+connected);
if (!connected) {
i=0;
}else{
new Thread() {
public void run() {
try {
/*chc add */
if(i==0){ //USB插入之后会重复的进行发送USB广播,所以进行处理,让他只进行一次设置。
i++;
Thread.sleep(500);
Log.d(TAG,"cuihangchao start()");
mUsbManager = mContext.getSystemService(UsbManager.class);
mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MTP);
mUsbManager.setUsbDataUnlocked(true);
}
} catch (Exception e) {
Log.d(TAG,"Exception="+e);
}
};
}.start();
}
/*chc end*/
onUsbMassStorageConnectionChangedAsync(available);
}
};
private final static String ACTION ="android.hardware.usb.action.USB_STATE"; //这是接受USB插拔广播的Action
此方法只是模拟的进行了一次MTP设置,所以会有BUG,当手机还没有开机就插入USB线的话,那么他并不会接受到这个广播,所以第一次还是仅充电,只有开机之后才会起效
解决连接USB 开机设置MTP
SystemUIService.java
private UsbManager mUsbManager;
@Override
public void onCreate() {
super.onCreate();
((SystemUIApplication) getApplication()).startServicesIfNeeded();
/*chc add */
new Thread() {
public void run() {
try {
Thread.sleep(15000);
Log.d("SystemUIService","onCreate cuihangchao");
mUsbManager =getSystemService(UsbManager.class);
mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MTP);
mUsbManager.setUsbDataUnlocked(true);
} catch (Exception e) {
Log.d("SystemUIService","Exception="+e);
}
};
}.start();
}
更新另一位大牛的写法:http://blog.csdn.net/u012846789/article/details/73195335
适用android 7
8.1
@Override
public void handleMessage(Message msg) {
...
case MSG_UPDATE_STATE:
...
if (!mConnected) {
// restore defaults when USB is disconnected
setEnabledFunctions(null, !mAdbEnabled, false);
setEnabledFunctions(UsbManager.USB_FUNCTION_MTP, false, true); //chc
add
}
...
case MSG_BOOT_COMPLETED:
mBootCompleted = true;
if (mPendingBootBroadcast) {
updateUsbStateBroadcastIfNeeded(false);
mPendingBootBroadcast = false;
}
//setEnabledFunctions(null, false, false);
setEnabledFunctions(UsbManager.USB_FUNCTION_MTP, false, true); //chc add
if (mCurrentAccessory != null) {
getCurrentSettings().accessoryAttached(mCurrentAccessory);
}
if (mDebuggingManager != null) {
mDebuggingManager.setAdbEnabled(mAdbEnabled);
}
break;
}
连接电脑,看电脑上MTP的图标显示之后,然后将延迟进行相应的调整
此问题到此结束