Android应用生死轮回的那些事儿(6) - 武器库完结篇-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

Android应用生死轮回的那些事儿(6) - 武器库完结篇

简介: PackageManager武器库的完结篇,让我们梳理一下从Android 1.0到Android 7.0的发展过程吧。

Android应用生死轮回的那些事儿(6) - 武器库完结篇

资源相关API

这些API虽然数量众多,但是功能相对简单,都是根据包头或者是ApplicationInfo或Activity信息去获取一些资源,如图标,文本等。

Android 1.0创始API

  • getXml:获取xml
  • getText:从资源中读取文本
  • getResourcesForApplication(app):获取资源对象
  • getResourcesForApplication(appPackageName):获取资源对象
  • getResourcesForActivity:从Activity中获取资源对象
  • getDrawable:获取Drawable
  • getDefaultActivityIcon:Activity的默认Icon
  • getApplicationLabel:获取Applicaiton的标签
  • getApplicationIcon:获取图标
  • getActivityIcon:获取图标

Android 2.3新增API

Android 2.3新增两个API,都是跟Logo相关。

  • getApplicationLogo
  • getActivityLogo

Android 4.4手表版新增API

手表上新增了Banner.

  • getApplicationBanner
  • getActivityBanner

Android 5.0新增API

Android 5.0新增了UserBadged UI,所以新增了三个方法:

  • getUserBadgedLabel
  • getUserBadgedIcon
  • getUserBadgedDrawableForDensity

禁掉或者解禁相关API

Android中可以支持将应用或者组件禁掉或者解禁。
ComponentEnableSetting是通过组件名来设置,ApplicationEnableSetting是通过包名。

  • setComponentEnabledSetting
  • getComponentEnabledSetting
  • setApplicationEnabledSetting
  • getApplicationEnabledSetting

setApplicationEnabledSetting

原型:

void setApplicationEnabledSetting (String packageName, int newState, int flags)

参数:

  • 包名
  • 新状态:状态请见下面的3种。
  • 标志位:要么是DONT_KILL_APP,要么是0.

状态可以为:

  • COMPONENT_ENABLED_STATE_ENABLED
  • COMPONENT_ENABLED_STATE_DISABLED
  • COMPONENT_ENABLED_STATE_DEFAULT

例程:禁掉微信:

    public void testSetApplicationEnableSetting() {
        mPm.setApplicationEnabledSetting("com.tencent.mm", PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }

setComponentEnabledSetting

将包中的四大组件之一禁掉或解禁。

原型:

void setComponentEnabledSetting (ComponentName componentName, int newState, int flags);

参数与上面的针对整个应用的一致。

检查签名

Android 1.0时,只能检查两个包名的签名是否一致。

原型:

int checkSignatures (String pkg1, String pkg2);

到了Android 2.0,也可以支持对两个uid之间判断签名。
问题是,从包名获取uid的API,要到Android 7.0才有呢。。。

原型:

int checkSignatures (int uid1, int uid2);

Prefer相关

addPackageToPreferred API已经在Android 2.1被除名,所以Prefer相关的API,只剩下下面三个:

  • getPreferredPackages
  • getPreferredActivities
  • clearPackagePreferredActivities

至此,Android 1.0的PackageManager提供的46个API全部讲完了。

feature相关API

Android 2.0开始引入feature的概念。我们可以通过getSystemAvailableFeatures方法获取系统支持哪些feature. 或者是通过hasSystemFeature(name)方法判断是否支持一个feature.
不过,Android 2.0的时候,feature是不分版本号的。支持版本号的或者是通过hasSystemFeature方法要到Android 7.0才会出品。

FeatureInfo类

属性:

  • flags: FLAG_REQUIRED或0
  • name:属性名
  • reqGlEsVersion:在name为空时,为需要的OpenGL ES的版本号
  • version:(Android 7.0才有)版本号

方法:

  • getGlEsVersion(): 0x00010002代表1.2

getSystemAvailableFeatures

原型:

FeatureInfo[] getSystemAvailableFeatures ();

例程:

    public void testGetSystemAvailableFeatures(){
        final FeatureInfo[] fis = mPm.getSystemAvailableFeatures();
        if(fis!=null){
            for(FeatureInfo fi:fis){
                Log.d(TAG,"Feature Info:"+fi.toString());
            }
        }
    }

输出结果:

08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{385e67d android.hardware.sensor.proximity fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{deb6972 android.hardware.sensor.accelerometer fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{788d1c3 android.hardware.faketouch fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{4fcea40 android.hardware.usb.accessory fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{c8f6d79 android.software.backup fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{102aabe android.hardware.touchscreen fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{b1cc71f android.hardware.touchscreen.multitouch fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{e66a6c android.software.print fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{2106835 android.software.voice_recognizers fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{ebab4ca android.hardware.sensor.gyroscope fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{6d0b63b android.hardware.bluetooth fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{b05c158 android.hardware.camera.autofocus fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{46c52b1 android.hardware.telephony.gsm fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{9375396 android.hardware.usb.host fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{40a7b17 android.hardware.audio.output fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{4c19b04 android.hardware.camera.flash fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{cc268ed android.hardware.camera.front fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{f0d1322 android.hardware.screen.portrait fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{2b1b1b3 android.software.home_screen fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{d3f6370 android.hardware.microphone fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{ce9a6e9 android.hardware.bluetooth_le fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{18d3f6e android.hardware.touchscreen.multitouch.jazzhand fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{f8bb60f android.software.app_widgets fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{e8f469c android.software.input_methods fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{32cc8a5 android.hardware.sensor.light fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{3d1e47a android.software.device_admin fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{457a42b android.hardware.camera fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{1983088 android.hardware.screen.landscape fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{dc64a21 android.software.managed_users fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{e89ce46 android.software.webview fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{daa5807 android.hardware.camera.any fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{4c3cd34 android.software.connectionservice fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{a1c675d android.hardware.touchscreen.multitouch.distinct fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{48488d2 android.hardware.location.network fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{28a6da3 android.hardware.wifi.direct fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{16a88a0 android.software.live_wallpaper fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{bbd1c59 android.hardware.location.gps fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{3fe601e android.hardware.wifi fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{9cc40ff android.hardware.location fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{bff8ecc android.hardware.telephony fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{71a2515 glEsVers=3.0 fl=0x0}

上面都是feature,最后一条是openGLES的版本号:3.0

hasSystemFeature

原型:

boolean hasSystemFeature (String name);

feature的取值可以是下面的字符串:

  • API 7 (Android 2.1)开始支持的feature

    • FEATURE_CAMERA
    • FEATURE_CAMERA_AUTOFOCUS
    • FEATURE_CAMERA_FLASH
    • FEATURE_LIVE_WALLPAPER
    • FEATURE_SENSOR_LIGHT
    • FEATURE_SENSOR_PROXIMITY
    • FEATURE_TELEPHONY
    • FEATURE_TELEPHONY_CDMA
    • FEATURE_TOUCHSCREEN_MULTITOUCH
  • API 8 (Android 2.2)开始支持的feature

    • FEATURE_BLUETOOTH
    • FEATURE_LOCATION
    • FEATURE_LOCATION_GPS
    • FEATURE_LOCATION_NETWORK
    • FEATURE_MICROPHONE
    • FEATURE_SENSOR_ACCELEROMETER
    • FEATURE_SENSOR_COMPASS
    • FEATURE_TOUCHSCREEN
    • FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT
    • FEATURE_WIFI
  • API 9 (Android 2.3)

    • FEATURE_AUDIO_LOW_LATENCY
    • FEATURE_CAMERA_FRONT
    • FEATURE_NFC
    • FEATURE_SENSOR_BAROMETER
    • FEATURE_SENSOR_GYROSCOPE
    • FEATURE_SIP
    • FEATURE_SIP_VOIP
    • FEATURE_TOUCHSCREEN_MULTITOUCH_JAZZHAND
  • API 11 (Android 3.0)

    • FEATURE_FAKETOUCH
  • API 12 (Android 3.1)

    • FEATURE_USB_ACCESSORY
    • FEATURE_USB_HOST
  • API 13 (Android 3.2)

    • FEATURE_FAKETOUCH_MULTITOUCH_DISTINCT
    • FEATURE_FAKETOUCH_MULTITOUCH_JAZZHAND
    • FEATURE_SCREEN_LANDSCAPE
    • FEATURE_SCREEN_PORTRAIT
  • API 14 (Android 4.0)

    • FEATURE_WIFI_DIRECT
  • API 17 (Android 4.2)

    • FEATURE_CAMERA_ANY
  • API 18 (Android 4.3)

    • FEATURE_APP_WIDGETS
    • FEATURE_BLUETOOTH_LE
    • FEATURE_HOME_SCREEN
    • FEATURE_INPUT_METHODS
  • API 19 (Android 4.4)

    • FEATURE_CONSUMER_IR
    • FEATURE_DEVICE_ADMIN
    • FEATURE_NFC_HOST_CARD_EMULATION
    • FEATURE_SENSOR_STEP_COUNTER
    • FEATURE_SENSOR_STEP_DETECTOR
  • API 20 (Android 4.4 Watch)

    • FEATURE_BACKUP
    • FEATURE_CAMERA_EXTERNAL
    • FEATURE_PRINTING
    • FEATURE_SENSOR_HEART_RATE
    • FEATURE_WATCH
    • FEATURE_WEBVIEW
  • API 21 (Android 5.0)

    • FEATURE_AUDIO_OUTPUT
    • FEATURE_CAMERA_CAPABILITY_MANUAL_POST_PROCESSING
    • FEATURE_CAMERA_CAPABILITY_MANUAL_SENSOR
    • FEATURE_CAMERA_CAPABILITY_RAW
    • FEATURE_CAMERA_LEVEL_FULL
    • FEATURE_CONNECTION_SERVICE
    • FEATURE_GAMEPAD
    • FEATURE_LEANBACK
    • FEATURE_LIVE_TV
    • FEATURE_MANAGED_USERS
    • FEATURE_OPENGLES_EXTENSION_PACK
    • FEATURE_SECURELY_REMOVES_USERS
    • FEATURE_SENSOR_AMBIENT_TEMPERATURE
    • FEATURE_SENSOR_HEART_RATE_ECG
    • FEATURE_SENSOR_RELATIVE_HUMIDITY
    • FEATURE_VERIFIED_BOOT
  • API 23 (Android 6.0)

    • FEATURE_AUDIO_PRO
    • FEATURE_AUTOMOTIVE
    • FEATURE_FINGERPRINT
    • FEATURE_HIFI_SENSORS
    • FEATURE_MIDI
  • API 24 (Android 7.0)

    • FEATURE_ETHERNET
    • FEATURE_FREEFORM_WINDOW_MANAGEMENT
    • FEATURE_NFC_HOST_CARD_EMULATION_NFCF
    • FEATURE_PICTURE_IN_PICTURE
    • FEATURE_VR_MODE
    • FEATURE_VR_MODE_HIGH_PERFORMANCE
    • FEATURE_VULKAN_HARDWARE_LEVEL
    • FEATURE_VULKAN_HARDWARE_VERSION

包名转换API - Android 2.2新增

原型:

String[] currentToCanonicalPackageNames (String[] names);
String[] canonicalToCurrentPackageNames (String[] names)

例程:

    public void testCurrentToCanonicalPackageNames(){
        String[] canonicalNames = mPm.currentToCanonicalPackageNames(new String[]{
                "com.yunos.xulun.testcppjni2","com.tencent.mm","com.taobao.taobao"});

        if(canonicalNames!=null) {
            for (String name : canonicalNames) {
                Log.d(TAG, "CanonicalName is:"+name);
            }
        }
    }

Android 1.0 PackageManager API总结

我们小结一下Android 1.0的46个PackageManager API:

  • 权限相关

    • removePermission
    • queryPermissionsByGroup
    • getPermissionInfo
    • getPermissionGroupInfo
    • getAllPermissionGroups
    • checkPermission
    • addPermission
  • 禁掉组件相关

    • setComponentEnabledSetting
    • getComponentEnabledSetting
    • setApplicationEnabledSetting
    • getApplicationEnabledSetting
  • 最佳匹配组件

    • resolveService
    • resolveContentProvider
    • resolveActivity
  • query组件

    • queryIntentServices
    • queryIntentActivityOptions
    • queryIntentActivities
    • queryBroadcastReceivers
    • queryInstrumentation
    • queryContentProviders
  • uid/gid相关

    • getPackagesForUid
    • getNameForUid
    • getPackageGids(packageName)
  • get组件集合

    • getInstalledPackages
    • getInstalledApplications
  • get单个组件

    • getPackageArchiveInfo
    • getInstrumentationInfo
    • getApplicationInfo
    • getActivityInfo
    • getPackageInfo
    • getServiceInfo
    • getReceiverInfo
  • 获取资源

    • getXml
    • getText
    • getResourcesForApplication(app)
    • getResourcesForApplication(appPackageName)
    • getResourcesForActivity
    • getDrawable
    • getDefaultActivityIcon
    • getApplicationLabel
    • getApplicationIcon
    • getActivityIcon
  • 最爱组件相关

    • getPreferredPackages
    • getPreferredActivities
    • clearPackagePreferredActivities
  • 比较签名

    • checkSignatures(pkg,pkg)

Android 1.5新增Package API总结

API 3 (Android 1.5)新增了3个API:

  • getLaunchIntentForPackage
  • isSafeMode
  • getSystemSharedLibraryNames

isSafeMode

是否处于安全模式。

getSystemSharedLibraryNames

返回so库的路径

Android 2.0新增API总结

API 5 (Android 2.0)新增4个API,前面已经都讲过了。

  • getInstallerPackageName
  • checkSignatures(uid1,uid2)
  • hasSystemFeature(name)
  • getSystemAvailableFeatures

Android 2.2新增API总结

API 8 (Android 2.2)新增3个API,2个名义包名转换的,还有一个权限管理的,前面都已经讲过了。

  • currentToCanonicalPackageNames
  • canonicalToCurrentPackageNames
  • addPermissionAsync

Android 2.3新增API总结

API 9 (Android 2.3)新增3个API,一个是getProviderInfo,终于把四大组件的getAPI凑齐了。另外从本版开始有了Logo的概念。

  • getProviderInfo
  • getApplicationLogo
  • getActivityLogo

Android 3.0新增API总结

API 11 (Android 3.0)只增加一个API,为2.0的getInstallerPackageName提供一个setter接口。

  • setInstallerPackageName

Android 4.0新增API总结

API 14 (Android 4.0)也只增加一个API,用于安装时校验。

  • verifyPendingInstall

Android 4.2新增API总结

API 17 (Android 4.2)只增加一个API,其实是对于上一个API的补足。

  • extendVerificationTimeout

Android 4.3新增API总结

API 18 (Android 4.3)新增一个权限相关API

  • getPackagesHoldingPermissions

Android 4.4新增API总结

API 19 (Android 4.4)只新增一个API,终于可以通过Intent查询ContentProvider了

  • queryIntentContentProviders

Android 4.4手表版新增API总结

API 20 (KITKAT_WATCH)新增两个Banner相关API.

  • getApplicationBanner
  • getActivityBanner

Android 5.0新增API总结

API 21 (Android 5.0)新增5个API,前三个是UserBadged界面相关。
第4个提供了PackageInstaller的接口。
最后一个是Leanback界面下获取Launch intent的API

  • getUserBadgedLabel
  • getUserBadgedIcon
  • getUserBadgedDrawableForDensity
  • getPackageInstaller
  • getLeanbackLaunchIntentForPackage

Android 6.0新增API总结

API 23 (Android 6.0)只增加一个权限相关API

  • isPermissionRevokedByPolicy

Android 7.0新增API总结

API 24 (Android 7.0)新增3个API。
systemm feature开始支持版本号。
终于可以获取package的uid了。

  • hasSystemFeature(name,version)
  • getPackageUid
  • getPackageGids(name,flags)

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章