安卓USB插入时系统识别然后直接用某个应用打开的方式
2015-12-15 20:28
387 查看
目的:USB插入的时候系统会提示是否要用某个APP来打开这个USB。
具体加入过程:
1、在manifest中加入intent-filter和meta-data
2、需要添加一个xml文件夹,然后在其中加入meta-data的resource
这样是识别所有的USB,也可以识别你设定的USB,需要列出vendor-id和product-id
具体的看file:///D:/sdk/docs/guide/topics/connectivity/usb/host.html
就这样就可以了。
intent-filter是一个intent的过滤器,见
http://developer.android.com/intl/zh-cn/guide/topics/manifest/intent-filter-element.html
首先,一个intent-filter里面必须要有一个action(可以有多个),这样才能知道我们这个filter在接收到什么intent的时候才会响应。
具体加入过程:
1、在manifest中加入intent-filter和meta-data
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.maowei.usb" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <uses-feature android:name="android.hardware.usb.host"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <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> </application> </manifest>
2、需要添加一个xml文件夹,然后在其中加入meta-data的resource
<?xml version="1.0" encoding="UTF-8"?> <resources> <usb-device/> </resources>
这样是识别所有的USB,也可以识别你设定的USB,需要列出vendor-id和product-id
具体的看file:///D:/sdk/docs/guide/topics/connectivity/usb/host.html
就这样就可以了。
intent-filter是一个intent的过滤器,见
http://developer.android.com/intl/zh-cn/guide/topics/manifest/intent-filter-element.html
首先,一个intent-filter里面必须要有一个action(可以有多个),这样才能知道我们这个filter在接收到什么intent的时候才会响应。
相关文章推荐
- 组合委托
- adb pull serial files
- 学习日记--fragment界面切换测试
- 切换电脑静态ip和动态ip
- 在c++代码中执行bat文件
- XenApp_XenDesktop_7.6实战篇之二十二:RDS授权配置
- 自己动手写cocos2dx游戏引擎(六)——引用计数机制
- 读书笔记-壹-《众妙之门-抓住访客心理的网页设计》
- 彻底理解子网掩码与子网划分
- 在Mac OSX下编译用于iOS的FreeType静态库
- C++中指针与引用的关系
- call和goto、eof、%~0怎么理解这段代码
- FaceBook pop 动画开源框架使用教程说明
- android 环境配置
- 获取 Activity 的返回参数
- oc特有语法
- SQL SERVER 内存学习系列(一)
- 设计模式-策略模式
- ExpandableListView
- C++通过基类指针delete派生类数组,析构函数是虚函数,程序为什么会崩溃? https://www.zhihu.com/question/30838092/answer/49623765