編輯:關於Android編程
1、android背景介紹
UsbService,在系統啟動時創建,在該文件中,和usb狀態息息相關的操作類是UsbDeviceManager,大部分的usb以及adb相關的邏輯,在這個類中做處理。UsbDeviceManager中,我們需要關注三部分內容。一、配置文件。二、 private final UEventObserver mUEventObserver = new UEventObserver() ,接受UEvent事件。三、UsbHandler。
其中配置文件,保存當前usb狀態。UEventObserver接受usb事件,push給UsbHandler來處理。UsbHandler是用來處理Usb事件的關鍵類。
2、Usb默認狀態從哪裡獲取,當前的默認狀態又是什麼?
Usb的當前狀態保存在mCurrentFunctions成員變量中,該變量在UsbHandler的構造中進行初始化。
mDefaultFunctions = SystemProperties.get(persist.sys.usb.config, adb);
// Check if USB mode needs to be overridden depending on OEM specific bootmode.
mDefaultFunctions = processOemUsbOverride(mDefaultFunctions);
// sanity check the sys.usb.config system property
// this may be necessary if we crashed while switching USB configurations
String config = SystemProperties.get(sys.usb.config, none);
if (!config.equals(mDefaultFunctions)) {
Slog.w(TAG, resetting config to persistent property: + mDefaultFunctions);
SystemProperties.set(sys.usb.config, mDefaultFunctions);
}
mCurrentFunctions = mDefaultFunctions;
3、插入usb時,彈出adb以及Notification通知的大體流程是什麼?
插入usb時,我們的UEventObserver會收到信息,最終push給UsbHandler。
UEvent信息:
04-17 14:20:03.352 V/UsbDeviceManager( 759): USB UEVENT: {USB_STATE=DISCONNECTED, SUBSYSTEM=android_usb, SEQNUM=7528, ACTION=change, DEVPATH=/devices/virtual/android_usb/android0}
然後執行UsbHandler的updateState方法。
private final UEventObserver mUEventObserver = new UEventObserver() {
@Override
public void onUEvent(UEventObserver.UEvent event) {
if (DEBUG) Slog.v(TAG, USB UEVENT: + event.toString());
String state = event.get(USB_STATE);
String accessory = event.get(ACCESSORY);
if (state != null) {
mHandler.updateState(state);
} else if (START.equals(accessory)) {
if (DEBUG) Slog.d(TAG, got accessory start);
startAccessoryMode();
}
}
};
updateState方法:更新mConnection狀態。
public void updateState(String state) {
int connected, configured;
if (DISCONNECTED.equals(state)) {
connected = 0;
configured = 0;
} else if (CONNECTED.equals(state)) {
connected = 1;
configured = 0;
} else if (CONFIGURED.equals(state)) {
connected = 1;
configured = 1;
} else {
Slog.e(TAG, unknown state + state);
return;
}
removeMessages(MSG_UPDATE_STATE);
Message msg = Message.obtain(this, MSG_UPDATE_STATE);
msg.arg1 = connected;
msg.arg2 = configured;
// debounce disconnects to avoid problems bringing up USB tethering
sendMessageDelayed(msg, (connected == 0) ? UPDATE_DELAY : 0);
}
最後在UsbHandler中進行更新狀態,進而彈出adb以及usb的Notification
case MSG_UPDATE_STATE:
mConnected = (msg.arg1 == 1);
mConfigured = (msg.arg2 == 1);
updateUsbNotification();
updateAdbNotification();
if (containsFunction(mCurrentFunctions,
UsbManager.USB_FUNCTION_ACCESSORY)) {
updateCurrentAccessory();
} else if (!mConnected) {
// restore defaults when USB is disconnected
setEnabledFunctions(mDefaultFunctions, false);
}
if (mBootCompleted) {
updateUsbState();
updateAudioSourceFunction();
}
break;
4、可設置的usb類型有哪些?
public static final String USB_FUNCTION_MASS_STORAGE = mass_storage;
/**
* Name of the adb USB function.
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
*
* {@hide}
*/
public static final String USB_FUNCTION_ADB = adb;
/**
* Name of the RNDIS ethernet USB function.
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
*
* {@hide}
*/
public static final String USB_FUNCTION_RNDIS = rndis;
/**
* Name of the MTP USB function.
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
*
* {@hide}
*/
public static final String USB_FUNCTION_MTP = mtp;
/**
* Name of the PTP USB function.
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
*
* {@hide}
*/
public static final String USB_FUNCTION_PTP = ptp;
/**
* Name of the CHARGING USB function.
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
*
* {@hide}
*/
public static final String USB_FUNCTION_CHARGING = charging;
/**
* Name of the audio source USB function.
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
*
* {@hide}
*/
public static final String USB_FUNCTION_AUDIO_SOURCE = audio_source;
/**
* Name of the Accessory USB function.
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
*
* {@hide}
*/
public static final String USB_FUNCTION_ACCESSORY = accessory;
這些是其定義。其中adb與charging不能同時兼容。
上層能夠設定usb類型的類名叫做:UsbSettings,在其中,用戶可以設定當前UsbSettings狀態為mtp、ptp以及廠商定義的其他usb狀態。
在用戶操作時,會調用mUsbManager.setCurrentFunction(function, true);
tring function = USB_FUNCTION_DEFAULT;
if (preference == mMtp && mMtp.isChecked()) {
function = UsbManager.USB_FUNCTION_MTP;
} else if (preference == mPtp && mPtp.isChecked()) {
function = UsbManager.USB_FUNCTION_PTP;
} else if (preference == mCharging && mCharging.isChecked()) {
function = UsbManager.USB_FUNCTION_CHARGING;
} else if (preference == mSDCard && mSDCard.isChecked()) {
function = UsbManager.USB_FUNCTION_MASS_STORAGE;
}
operateInprogress = true;
mUsbManager.setCurrentFunction(function, true);
updateToggles(function);
USBManager會繼而調用UsbService的setCurrentFunction
UsbService.java
@Override
public void setCurrentFunction(String function, boolean makeDefault) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
// If attempt to change USB function while file transfer is restricted, ensure that
// the current function is set to none, and return.
UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
if (userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
if (mDeviceManager != null) mDeviceManager.setCurrentFunctions(none, false);
return;
}
if (mDeviceManager != null) {
mDeviceManager.setCurrentFunctions(function, makeDefault);
} else {
throw new IllegalStateException(USB device mode not supported);
}
}
最終還是會調用UsbDeviceManager中的setCurrentFunction。
Android仿微信實現下拉列表
本文要實現微信6.1中點擊頂部菜單欄的“+”號按鈕時,會彈出一個列表框。這裡用的了Activity實現,其實最好的方法可以用ActionBar,不過這貨好像只
Android控件之ListView用法實例詳解
本文實例講述了Android控件之ListView用法。分享給大家供大家參考。具體如下:示例一:在android開發中ListView是比較常用的組件,它以列表的形式展示
Android開發之內存管理
概念應用的開發離不開存儲,存儲分為網絡、內存、SDCard文件存儲以及外部SDCard2文件存儲,開發中一定要注意好內存管理以免oom、卡頓等不好的用戶體驗,同時還要注意
Android開發:ListView控件:給Item綁定了點擊事件,卻點擊無效
一.問題引入ListView控件:給Item綁定了點擊事件,卻點擊無效。二.解決方案ListView使用了自定義布局文件,在布局文件中有button等控件時,這些控件獲取