Android_Manager:PackageManager

本文详细介绍了Android中的包管理器(PackageManager)及其使用方法。包括如何通过PackageManager获取单个或多个应用的PackageInfo,如何从PackageInfo中提取应用名称、图标、安装路径等信息,并提供了判断系统应用和启动应用的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述这里写图片描述

相关类介绍

PackageManager不属于系统级服务,放在这个类别里是因为它也是xManager

PackageManager:包管理者,可以获取单个应用的PackageInfo,可以获取多个应用的PackageInfo集合。
PackageInfo:包含versionCode+versionName+ApplicationInfo
ApplicationInfo:包含应用的名称+图标+安装包路径+包名

PackageInfo

PackageManager pm = getPackageManager();  
PackageInfo packageInfo = pm.getPackageInfo("com.android.mobilesafe", 0);//??  
localVersionCode = packageInfo.versionCode;  
localVersionName = packageInfo.versionName;  
tv_version_name.setText(localVersionName);

获取已经安装的所有包

PackageManager pm = getPackageManager();
List<PackageInfo> packages = pm.getInstalledPackages(0);

ApplicationInfo

获取应用详情

for (PackageInfo packageInfo : packages) {
    ApplicationInfo info = packageInfo.applicationInfo;
    Drawable icon = info.loadIcon(pm);
    String label = info.loadLabel(pm).toString();
    String name = info.name;
    String sourceDir = info.sourceDir;
    String packageName = packageInfo.packageName;
}

loadIcon(pm):才是图标,loadLogo(pm)不是。
label:才是应用的名称.
name:值是null
sourceDir: apk的路径
packageName:包名

String name = info.name;//null
sourceDir=/data/app/so.ofo.labofo-1/base.apk
label=ofo共享单车
packageName=so.ofo.labofo

判断是不是系统应用?结果为true就是系统应用。

(applicationIno.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM
if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
    bean.isSystem = true;
} 

判断应用安装再手机还是SD卡?结果为true就是系统应用。

(info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE

启动APP

PackageManager pm = getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage(info.getPackageName());
if (launchIntent != null) {
    startActivity(launchIntent);
}

其它

Demo:https://git.oschina.net/MobileSafeDemo/RuanJianGuanLi

E SystemServiceRegistry: Manager wrapper not available: persistent_data_block 03-25 10:15:02.751 1000 829 870 I am_wtf : [0,21183,system_server,-1,SystemServiceRegistry,No service published for: persistent_data_block] 03-25 10:15:02.752 1000 829 870 I am_wtf : [0,21183,system_server,-1,SystemServiceRegistry,Manager wrapper not available: persistent_data_block] 03-25 10:15:02.753 1000 829 860 I commit_sys_config_file: [package-session,2] 03-25 10:15:02.753 1000 829 962 E AtomicFile: Failed to rename /data/system/install_sessions.xml.new to /data/system/install_sessions.xml 03-25 10:15:02.753 1000 829 962 I commit_sys_config_file: [package-session,2] 03-25 10:15:02.809 1000 829 1017 W RuleEvaluation: Integrity rule files are not available. 03-25 10:15:02.810 1000 829 897 I PackageManager: Integrity check passed for file:///data/app/vmdl46588232.tmp 03-25 10:15:02.813 1000 829 860 I commit_sys_config_file: [package-session,3] 03-25 10:15:02.814 1000 829 897 V PackageManager: pkgLite for install: PackageInfoLite{6c934c5 android.inputmethodservice.cts.devicetest} 03-25 10:15:02.824 1000 829 897 W PackageManager: Can't replace full app with instant app: android.inputmethodservice.cts.devicetest for user: 0 03-25 10:15:02.824 1000 829 870 I ActivityManager: Force stopping android.inputmethodservice.cts.devicetest appid=10183 user=-1: installPackageLI 03-25 10:15:02.824 1000 829 897 W PackageManager: Failed to prepare for install. 03-25 10:15:02.828 1000 829 897 D PackageInstallerSession: Marking session 46588232 as failed: INSTALL_FAILED_SESSION_INVALID: Failed to prepare for install. 03-25 10:15:02.828 1000 829 897 I PackageInstallerSession: Session [46588232] was destroyed because of [Session marked as failed: INSTALL_FAILED_SESSION_INVALID: Failed to prepare for install.] 03-25 10:15:02.830 1000 829 962 I commit_sys_config_file: [package-session,1] 03-25 10:15:02.832 1000 829 860 I commit_sys_config_file: [package-session,2] 03-25 10:15:03.026 shell 520 520 E adbd : longfei transport UsbFfs opening service: shell,v2,TERM=xterm-256color,raw:appops write-settings 03-25 10:15:03.057 1000 829 4479 I commit_sys_config_file: [appops_accesses,5]
03-27
Line 3507: 01-01 19:26:38.540 01744 01744 I adbd : persist.adb.watchdog set to '' Line 3508: 01-01 19:26:38.542 01744 01744 I adbd : persist.sys.test_harness set to '' Line 3510: 01-01 19:26:38.542 01744 01744 I adbd : adb watchdog timeout set to 600 seconds Line 3511: 01-01 19:26:38.543 01744 01744 I adbd : adbd started Line 3511: 01-01 19:26:38.543 01744 01744 I adbd : adbd started Line 3513: 01-01 19:26:38.543 01744 01759 I adbd : opening control endpoint /dev/usb-ffs/adb/ep0 Line 3514: 01-01 19:26:38.544 01744 01757 I adbd : Waiting for persist.adb.tls_server.enable=1 Line 3517: 01-01 19:26:38.554 01744 01759 I adbd : UsbFfsConnection constructed Line 3526: 01-01 19:26:38.570 01744 01761 I adbd : UsbFfs-monitor thread spawned Line 4331: 01-01 19:26:39.572 01744 01761 W adbd : timed out while waiting for FUNCTIONFS_BIND, trying again Line 4332: 01-01 19:26:39.572 01744 01761 I adbd : UsbFfs: connection terminated: monitor thread finished Line 4333: 01-01 19:26:39.573 01744 01744 I adbd : UsbFfs: already offline Line 4334: 01-01 19:26:39.573 01744 01744 I adbd : destroying transport UsbFfs Line 4335: 01-01 19:26:39.573 01744 01744 I adbd : UsbFfsConnection being destroyed Line 4353: 01-01 19:26:39.642 01744 01759 I adbd : opening control endpoint /dev/usb-ffs/adb/ep0 Line 4355: 01-01 19:26:39.648 01744 01759 I adbd : UsbFfsConnection constructed Line 4363: 01-01 19:26:39.652 01744 01965 I adbd : UsbFfs-monitor thread spawned Line 6044: 01-01 19:26:40.669 01744 01965 W adbd : timed out while waiting for FUNCTIONFS_BIND, trying again Line 6045: 01-01 19:26:40.669 01744 01965 I adbd : UsbFfs: connection terminated: monitor thread finished Line 6046: 01-01 19:26:40.670 01744 01744 I adbd : UsbFfs: already offline Line 6047: 01-01 19:26:40.670 01744 01744 I adbd : destroying transport UsbFfs Line 6048: 01-01 19:26:40.670 01744 01744 I adbd : UsbFfsConnection being destroyed Line 6641: 01-01 19:26:40.810 01744 01759 I adbd : opening control endpoint /dev/usb-ffs/adb/ep0 Line 6703: 01-01 19:26:40.815 01744 01759 I adbd : UsbFfsConnection constructed Line 6726: 01-01 19:26:40.820 01744 02220 I adbd : UsbFfs-monitor thread spawned Line 7858: 01-01 19:26:41.384 01744 02220 I adbd : USB event: FUNCTIONFS_BIND Line 12899: 01-01 19:26:45.298 02952 02958 E Diag_Lib: DPL : gIsQXDMDisabled 0, gIsADBDisabled 0, gIsDebugDisabled 0, gIsIMSLogsDisabled 0 gIsDebugDataPathDisabled = 0 Line 13319: 01-01 19:26:45.317 02952 02958 E Diag_Lib: DPL : gIsQXDMDisabled 0, gIsADBDisabled 0, gIsDebugDisabled 0, gIsIMSLogsDisabled 0 gIsDebugDataPathDisabled = 0 Line 13978: 01-01 19:26:45.534 02924 02924 D CompatConfig: No directory /apex/com.android.adbd/etc/compatconfig, skipping Line 17407: 01-01 19:26:47.093 02924 02924 W PackageManager: No package known for package restrictions com.google.android.adbd Line 17766: 01-01 19:26:47.274 02924 02924 D PackageManager: Directories scanned as system partitions: [/system:0, /vendor:524288, /odm:4194304, /oem:262144, /product:1048576, /system_ext:2097152, /my_product/non_overlay:1048576, /mnt/opex/com.oplus.odmf@15002009:2097152, /mnt/opex/com.oplus.moduleservices@15000014:2097152, /mnt/opex/com.oplus.fancyIconLoader@1000000:2097152, /mnt/opex/com.oplus.NetworkAssistSys@15002031:2097152, /mnt/opex/com.oplus.CustCore@15000029:2097152, /my_company:1048576, /my_carrier:1048576, /my_bigball:1048576, /my_product/cust/TR:1048576, /apex/com.android.ipsec:41943040, /apex/com.android.ondevicepersonalization:41943040, /apex/com.android.cellbroadcast:41943040, /apex/com.android.profiling:41943040, /apex/com.android.nfcservices:41943040, /apex/com.android.media.swcodec:41943040, /apex/com.android.conscrypt:41943040, /apex/vendor.oplus.hardware.stability.aidl:42467328, /apex/com.android.os.statsd:41943040, /apex/com.android.wifi:41943040, /apex/com.android.uwb:41943040, /apex/com.android.virt:41943040, /apex/com.android.adservices:41943040, /apex/com.android.configinfrastructure:41943040, /apex/com.android.devicelock:41943040, /apex/com.android.appsearch:41943040, /apex/com.google.mainline.primary.libs:41943040, /apex/com.android.compos:44040192, /apex/com.android.tethering:41943040, /apex/com.android.resolv:41943040, /apex/com.android.mediaprovider:41943040, /apex/com.android.runtime:41943040, /apex/com.android.neuralnetworks:41943040, /apex/com.android.permission:41943040, /apex/com.android.adbd:41943040, /apex/com.android.i18n:41943040, /apex/com.android.scheduling:41943040, /apex/com.android.media:41943040, /apex/com.android.tzdata:41943040, /apex/vendor.oplus.hardware.engineer.aidl:42467328, /apex/com.android.sdkext:41943040, /apex/com.android.rkpd:41943040, /apex/com.android.apex.cts.shim:41943040, /apex/com.android.art:41943040, /apex/com.android.extservices:41943040, /apex/com.android.vndk.v33:44040192, /apex/com.android.healthfitness:41943040] Line 17819: 01-01 19:26:47.410 02924 02924 I PackageManager: /data/apex/decompressed/com.android.adbd@351010000.decompressed.apex changed; collecting certs Line 17823: 01-01 19:26:47.422 02924 02924 E PackageManagerServiceUtilsExtImpl: Fix up user restrict data of pkg: com.google.android.adbd Line 19413: 01-01 19:26:51.224 02924 02924 D PackageManager: No files in app dir /apex/com.android.adbd/priv-app Line 19414: 01-01 19:26:51.224 02924 02924 D PackageManager: No files in app dir /apex/com.android.adbd/app Line 20634: 01-01 19:26:52.985 02924 02924 W PackageSettings: Couldn't write -1 to /config/sdcardfs/com.google.android.adbd/appid Line 21520: 01-01 19:26:53.270 02924 02924 D OverlayManager: -> Updating overlay: target=com.google.android.adbd overlays=[OverlayPaths { resourceDirs = [], overlayPaths = [/data/resource-cache/com.android.systemui-neutral-coSn.frro, /data/resource-cache/com.android.systemui-accent-n7sV.frro, /data/resource-cache/com.android.systemui-dynamic-EysW.frro] }] userId=0 Line 77814: 01-01 19:27:08.471 01744 01758 I adbd : adbd_auth: received a new framework connection Line 77814: 01-01 19:27:08.471 01744 01758 I adbd : adbd_auth: received a new framework connection Line 77815: 01-01 19:27:08.472 01744 01758 I adbd : adbd_auth: received new framework fd 16 (current = -1) Line 77815: 01-01 19:27:08.472 01744 01758 I adbd : adbd_auth: received new framework fd 16 (current = -1) Line 81135: 01-01 19:27:09.113 04893 04893 D vold_prepare_subdirs: Setting up mode 771 uid 0 gid 1000 context u:object_r:apex_module_data_file:s0 on path: /data/misc_ce/0/apexdata/com.android.adbd Line 82329: 01-01 19:27:09.362 02924 04870 W PackageManager: Skipping preparing app data for com.google.android.adbd
最新发布
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值