当USB设备断开Android上检测 [英] Detecting when a USB device is detached on Android

查看:890
本文介绍了当USB设备断开Android上检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,需要检测时,USB外设连接或分离。它工作正常时,外设先附着,但我没有收到任何通知(即我没有收到意图的动作是 ACTION_USB_DEVICE_DETACHED )时,它随后被分离。

I've got an Android app that needs to detect when a USB peripheral is attached or detached. It works fine when the peripheral is first attached, but I don't receive any notification (i.e., I don't receive an Intent whose action is ACTION_USB_DEVICE_DETACHED) when it is subsequently detached.

下面是相关的部分我的Andr​​oidManifest.xml

<activity android:name=".LauncherActivity">
    <intent-filter>
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
    </intent-filter>
    <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />
    <meta-data android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" android:resource="@xml/device_filter" />
</activity>

这也可能是值得一提的是, LauncherActivity 只存在启动服务当设备连接,并停止服务时,它被分离。在这两种情况下, LauncherActivity 总是完成 ES自身立即。在 LauncherActivity.onCreate 所有这一切发生。

It may also be worth noting that LauncherActivity only exists to start a Service when the device is attached, and to stop the service when it is detached. In either case, LauncherActivity always finishes itself immediately. All of this occurs in LauncherActivity.onCreate.

任何想法?

推荐答案

USB_DEVICE_DETACHED是播出的意图,因此,你可能要宣布的BroadcastReceiver清单中有相应的意图过滤器分离的动作,也有元数据的附加。 同去的USB_ACCESSORY_DETACHED,对于谁是有兴趣的。

USB_DEVICE_DETACHED is a broadcast intent, thus you may want to declare the BroadcastReceiver in manifest with the appropriate intent-filter for detached action, also with meta-data attached. Same goes for USB_ACCESSORY_DETACHED, for who is interested.

摘要:
USB_XXX_ATTACHED 是一种活动的意图
USB_XXX_DETACHED 是广播意图

Summary:
USB_XXX_ATTACHED is an activity intent
USB_XXX_DETACHED is a broadcast intent

(其中XXX =设备|附件)

(where XXX = DEVICE | ACCESSORY)

请参阅: http://developer.android.com/guide/components/意图-filters.html

没有在这些消息传送系统没有重叠:广播意图传递只广播接收机,从来没有活动或服务

"There is no overlap within these messaging systems: Broadcast intents are delivered only to broadcast receivers, never to activities or services"

这篇关于当USB设备断开Android上检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆