android程序中的AndroidManifest.xml中的uses-feature详解

来源:互联网 发布:web前端开发js面试宝典 编辑:程序博客网 时间:2024/06/08 17:56

【整理】android程序中的AndroidManifest.xml中的uses-feature详解

【已解决】Android设备作为Host希望实现可以检测到USB设备插入

期间,去学习一下AndroidManifest.xml中的uses-feature:

http://developer.android.com/guide/topics/manifest/uses-feature-element.html

AndroidManifest.xml中的uses-feature

在android的manifest文件:

用于指定android程序,是否需要某种硬件或软件资源/功能。

uses-feature的语法

<uses-feature  android:name="string"  android:required=["true" | "false"]  android:glEsVersion="integer" />

android:name

  • 摄像头: android.hardware.camera
  • 各种传感器:
    • 加速计 :android.hardware.sensor.accelerometer
    • 气压计 :android.hardware.sensor.barometer
    • 指南针 android.hardware.sensor.compass
    • 陀螺仪 android.hardware.sensor.gyroscope
    • 感光 android.hardware.sensor.light
    • 近距离感测 android.hardware.sensor.proximity
  • 麦克风: android.hardware.microphone
  • 定位: android.hardware.location
  • USB:
    • USB Host: android.hardware.usb.host
  • WIFI: android.hardware.wifi
  • 蓝牙: android.hardware.bluetooth
  • Bluetooth Low Energy: android.software.bluetooth_le
  • VOIP: android.software.sip.voip

http://developer.android.com/guide/topics/manifest/uses-feature-element.html#features-reference

android:required

  • android:required="true" :表示需要设备具有某个功能。
    • 如果设备没有该功能,则程序不工作,就是可以理解的
  • android:required="false" :表示希望设备,最好具有某个功能
    • 设备即使没有该功能,程序也应该可以正常工作
    • 为了程序工作的更好,最好具有该功能。

如果不指定,默认为true:

android:required="true":

android:qlEsVersion

  • OpenGL ES 1.0
  • OpenGL ES 2.0
  • OpenGL ES 3.0

我这里不涉及到,不去深究。

uses-feature写法举例

1.某程序需要蓝牙和摄像头,就可以这么写:

<uses-feature android:name="android.hardware.bluetooth" /><uses-feature android:name="android.hardware.camera" />

2.如果,某个设备,是类似于照相机之类的程序,那么没有摄像头,就没法正常工作,则可以加上required参数为true:

<uses-feature android:name="android.hardware.camera" android:required="true" />

3.如果某个文件共享的软件,除了通过WIFI传输外,如果有蓝牙,那最好,也支持通过蓝牙传输,则可以加上required为false,希望最好有蓝牙:

<uses-feature android:name="android.hardware.bluetooth" android:required="false" />

4.比如,我的程序,是将android设备作为USB的Host,外接USB的设备的,所以要求必须有USB的host,所以可以写为:

<uses-feature android:required="true" android:name="android.hardware.usb.host" />

uses-feature的注意事项和其他说明

uses-feature,只是起到指示性的作用,不是强制的检测

uses-feature,只是起到指示性的作用,不是强制的检测

如果像上面的,我写了:

<uses-feature android:name="android.hardware.usb.host" android:required="true" />

[2013-11-05 10:15:37 - com.android.ide.eclipse.adt.internal.project.AndroidManifestHelper] Parser exception for D:\DevRoot\xxxxxxxxxxxxxx\AndroidManifest.xml: Element type "uses-feature" must be followed by either attribute specifications, ">" or "/>".

[2013-11-05 10:15:37 - yyyy] Error in an XML file: aborting build.

<uses-feature android:required="true" android:name="android.hardware.usb.host" />

本意是:希望此android设备必须有usb的host,否则没法工作。

即使没有usb的host,该app,也是可以在该android设备上面跑的

只不过不能正常工作罢了。

而作为Android系统,是不是强制去检测:

当此android设备,没有usb的host,就不让该app运行。

写android的app时最好还是加上合适的uses-feature的说明比较好

对于其他一些程序,比如Google Play

会根据你的程序中的uses-feature的声明,去过滤,分类android的app的。

另外,你的程序中,也最好,根据实际需要,去加上合适的uses-feature的说明,比较好。

方便用户和其他人明白,你的app对于资源的需求:

至少间接的相当于:给当前app,弄了个prerequisite前提条件了。

便于用户清楚需要哪些软硬件条件,才能运行你的当前的app。

可以用aapt去检测android的app(xxx.apk)中的uses-feature属性

Testing the features required by your application

adt-bundle-windows/sdk/platform-tools/aapt.exe

去拷贝了某个apk,测试了一把,结果如下:

CLi@PC-CLI-1 /cygdrive/d/DevRoot/android/adt-bundle-windows/sdk/platform-tools$ lsaapt.exe*  AdbWinApi.dll*     aidl.exe*  crifanLiTestAapt.apk*    crifanLiTestAapt_3.apk*  dx.bat*        lib/             NOTICE.txt*    source.properties*adb.exe*   AdbWinUsbApi.dll*  api/       crifanLiTestAapt_2.apk*  dexdump.exe*             fastboot.exe*  llvm-rs-cc.exe*  renderscript/CLi@PC-CLI-1 /cygdrive/d/DevRoot/android/adt-bundle-windows/sdk/platform-tools$ ./aapt dump badging crifanLiTestAapt_3.apkpackage: name='com.mm.xxxxxxx' versionCode='1' versionName='1.0'sdkVersion:'14'targetSdkVersion:'17'uses-permission:'android.permission.WRITE_EXTERNAL_STORAGE'uses-feature:'android.hardware.usb.host'application-label:'MobileHandHeld'application-label-zh:'某android应用程序'application-icon-160:'res/drawable-mdpi/yyyyyyyyyy.png'application-icon-240:'res/drawable-hdpi/yyyyyyyyyy.png'application-icon-320:'res/drawable-xhdpi/yyyyyyyyyy.png'application-icon-480:'res/drawable-xxhdpi/yyyyyyyyyy.png'application: label='MobileHandHeld' icon='res/drawable-mdpi/yyyyyyyyyy.png'launchable-activity: name='com.mm.xxxxxxx.activities.DeviceListActivity'  label='MobileHandHeld' icon=''uses-permission:'android.permission.READ_EXTERNAL_STORAGE'uses-implied-permission:'android.permission.READ_EXTERNAL_STORAGE','requested WRITE_EXTERNAL_STORAGE'uses-feature:'android.hardware.touchscreen'uses-implied-feature:'android.hardware.touchscreen','assumed you require a touch screen unless explicitly made optional'uses-feature:'android.hardware.screen.landscape'uses-implied-feature:'android.hardware.screen.landscape','one or more activities have specified a landscape orientation'mainother-activitiessupports-screens: 'small' 'normal' 'large' 'xlarge'supports-any-density: 'true'locales: '--_--' 'zh'densities: '160' '240' '320' '480'

aapt dump badging apk within cygwin

1.我这里是在cygwin下运行aapt的。

你可以在windows的cmd下,效果是一样的,只不过貌似中文会有乱码。。。

总结

Android的文档,解释的还算蛮清楚的。

0 0
原创粉丝点击