Android 获取安装的app

在Android逆向中,很多目标app会获取设备安装的app列表来判断设备的环境是否干净,下面将会从几个点分析如何获取设备安装的app

PackageManager

使用 PackageManager 可以获取设备上所有已安装的应用程序信息

PackageManager packageManager = getPackageManager();
List<ApplicationInfo> apps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo appInfo : apps) {
    String appName = packageManager.getApplicationLabel(appInfo).toString();
    String packageName = appInfo.packageName;
    // 其他信息如版本号等也可以通过 appInfo 获取
}

调用PackageManager的getPackageInfo来判断app是否存在

public boolean isAppInstalled(String packageName) {
    PackageManager pm = getPackageManager();
    try {
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

调用PackageManager的getApplicationInfo来判断app是否存在

PackageManager pm = getPackageManager();
ApplicationInfo appInfo = null;
try {
    appInfo = pm.getApplicationInfo("com.example.myapp", 0);
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}
boolean isInstalled = (appInfo != null);

Intent

通过发送一个隐式的 Intent ,你可以请求系统返回已安装应用的信息

Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> apps = getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo appInfo : apps) {
    String appName = appInfo.loadLabel(getPackageManager()).toString();
    String packageName = appInfo.activityInfo.packageName;
}

检测单个app是否存在

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setPackage("com.example.myapp");  // 替换成要检查的应用的包名
List<ResolveInfo> activities = getPackageManager().queryIntentActivities(intent, 0);
boolean isInstalled = !activities.isEmpty();

你可能感兴趣的:(android)