我正在检测是否使用广播接收器在Android设备中安装了应用程序。
在我检测到安装了应用程序的那段时间里,我想获取该应用程序的应用程序名称。
我试图在网上搜索,但我只能找到如何安装所有应用程序,有人可以帮助我吗?
答案 0 :(得分:1)
使用此代码,您可以获取已安装的应用程序列表以及最近安装/更新软件包的日期。
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
String packageName = packageInfo.packageName;
String appFile = packageInfo.sourceDir;
long lastModified = new File(appFile).lastModified();
//Use this to get first time install time
//long installed = context.getPackageManager().getPackageInfo(packageName, 0).firstInstallTime;
Log.d(TAG, "Installed package :" + packageName);
Log.d(TAG, "Source dir : " + appFile);
Log.d(TAG, "Last Modified Time :" + lastModified);
}
答案 1 :(得分:1)
我添加了一个简单的逻辑,它将获取最近安装的应用程序(appName和packageName)。
PackageManager pm = context.getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
long prev_modified = 0;
packageName = null;
appName = null;
for (ApplicationInfo packageInfo : packages) {
String appFile = packageInfo.sourceDir;
long lastModified = new File(appFile).lastModified();
//Use this to get first time install time
//long installed = context.getPackageManager().getPackageInfo(packageName, 0).firstInstallTime;
if (lastModified > prev_modified) {
prev_modified = lastModified;
packageName = packageInfo.packageName;
appName = packageInfo.loadLabel(context.getPackageManager()).toString();
}
}
Toast.makeText(context, packageName + "\n" + appName, Toast.LENGTH_SHORT).show();