Android 3.1 Activity, how to receive a ACTION_USB_DEVICE_ATTACHED?
I'm trying to make an application u开发者_运维技巧sing the USB Host provided by Android Honeycomb. What I want is to know when my USB Device (USB flashdrive) is connected to the main device. For now I'm able to know when the USB device is detached but not when it is attached.
It seems that those 2 intents are not sent in the same way.
Could you please help me? Thanks a lot.
From what I can tell, you'll have to make a proxy Activity that intercepts the USB_DEVICE_ATTACHED intent, then starts up the real activity, passing it some intent, and finally finish() is called.
Example XML ripped from GalleryNew3DGoogle.apk
<activity android:label="@string/app_name"
android:name="com.android.gallery3d.app.UsbDeviceActivity" android:taskAffinity=""
android:launchMode="singleInstance"><intent-filter><action
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/></intent-filter><meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter"/></activity>
And here is the .smali from UsbDeviceActivity
(the only method in there is onCreate
)
.method protected onCreate(Landroid/os/Bundle;)V
.locals 3
.parameter
.prologue
.line 35
invoke-super {p0, p1}, Landroid/app/Activity;->onCreate(Landroid/os/Bundle;)V
.line 38
new-instance v0, Landroid/content/Intent;
const-class v1, Lcom/android/gallery3d/app/Gallery;
invoke-direct {v0, p0, v1}, Landroid/content/Intent;-><init>(Landroid/content/Context;Ljava/lang/Class;)V
.line 39
const/high16 v1, 0x1400
invoke-virtual {v0, v1}, Landroid/content/Intent;->addFlags(I)Landroid/content/Intent;
.line 41
:try_start_0
invoke-virtual {p0, v0}, Lcom/android/gallery3d/app/UsbDeviceActivity;->startActivity(Landroid/content/Intent;)V
:try_end_0
.catch Landroid/content/ActivityNotFoundException; {:try_start_0 .. :try_end_0} :catch_0
.line 45
:goto_0
invoke-virtual {p0}, Lcom/android/gallery3d/app/UsbDeviceActivity;->finish()V
.line 46
return-void
.line 42
:catch_0
move-exception v0
.line 43
const-string v1, "UsbDeviceActivity"
const-string v2, "unable to start Gallery activity"
invoke-static {v1, v2, v0}, Landroid/util/Log;->e(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)I
goto :goto_0
.end method
精彩评论