PackageManager武器库的完结篇,让我们梳理一下从Android 1.0到Android 7.0的发展过程吧。
这些API虽然数量众多,但是功能相对简单,都是根据包头或者是ApplicationInfo或Activity信息去获取一些资源,如图标,文本等。
Android 2.3新增两个API,都是跟Logo相关。
手表上新增了Banner.
Android 5.0新增了UserBadged UI,所以新增了三个方法:
Android中可以支持将应用或者组件禁掉或者解禁。
ComponentEnableSetting是通过组件名来设置,ApplicationEnableSetting是通过包名。
原型:
void setApplicationEnabledSetting (String packageName, int newState, int flags)
参数:
状态可以为:
例程:禁掉微信:
public void testSetApplicationEnableSetting() {
mPm.setApplicationEnabledSetting("com.tencent.mm", PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
将包中的四大组件之一禁掉或解禁。
原型:
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);
addPackageToPreferred API已经在Android 2.1被除名,所以Prefer相关的API,只剩下下面三个:
至此,Android 1.0的PackageManager提供的46个API全部讲完了。
Android 2.0开始引入feature的概念。我们可以通过getSystemAvailableFeatures方法获取系统支持哪些feature. 或者是通过hasSystemFeature(name)方法判断是否支持一个feature.
不过,Android 2.0的时候,feature是不分版本号的。支持版本号的或者是通过hasSystemFeature方法要到Android 7.0才会出品。
属性:
方法:
原型:
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
原型:
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
原型:
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的46个PackageManager API:
API 3 (Android 1.5)新增了3个API:
是否处于安全模式。
返回so库的路径
API 5 (Android 2.0)新增4个API,前面已经都讲过了。
API 8 (Android 2.2)新增3个API,2个名义包名转换的,还有一个权限管理的,前面都已经讲过了。
API 9 (Android 2.3)新增3个API,一个是getProviderInfo,终于把四大组件的getAPI凑齐了。另外从本版开始有了Logo的概念。
API 11 (Android 3.0)只增加一个API,为2.0的getInstallerPackageName提供一个setter接口。
API 14 (Android 4.0)也只增加一个API,用于安装时校验。
API 17 (Android 4.2)只增加一个API,其实是对于上一个API的补足。
API 18 (Android 4.3)新增一个权限相关API
API 19 (Android 4.4)只新增一个API,终于可以通过Intent查询ContentProvider了
API 20 (KITKAT_WATCH)新增两个Banner相关API.
API 21 (Android 5.0)新增5个API,前三个是UserBadged界面相关。
第4个提供了PackageInstaller的接口。
最后一个是Leanback界面下获取Launch intent的API
API 23 (Android 6.0)只增加一个权限相关API
API 24 (Android 7.0)新增3个API。
systemm feature开始支持版本号。
终于可以获取package的uid了。