我想确定安装在手机内存上并可移动到 SD 卡的应用程序列表。 我正在使用以下函数来获取已安装应用程序的列表。
我已经检查了 Android 源代码中的 InstalledAppDetails 但无法获得解决方案。
提前致谢
private List<App> loadInstalledApps(boolean includeSysApps)
{
List<App> apps = new ArrayList<App>();
// the package manager contains the information about all installed apps
PackageManager packageManager = getPackageManager();
List<PackageInfo> packs = packageManager.getInstalledPackages(0); //PackageManager.GET_META_DATA
for(int i = 0; i < packs.size(); i++)
{
PackageInfo p = packs.get(i);
ApplicationInfo a = p.applicationInfo;
// skip system apps if they shall not be included
if ((!includeSysApps) && ((a.flags & ApplicationInfo.FLAG_SYSTEM) == 1))
{
continue;
}
App app = new App();
app.setTitle(p.applicationInfo.loadLabel(packageManager).toString());
app.setPackageName(p.packageName);
app.setVersionName(p.versionName);
app.setVersionCode(p.versionCode);
app.setInstallDir(p.applicationInfo.sourceDir);
app.setInstallSize(calculateSize(app.getInstallDir()));
CharSequence description = p.applicationInfo.loadDescription(packageManager);
app.setDescription(description != null ? description.toString() : "");
if(app.getInstallDir().contains("/mnt"))
{
//app on sdcard
apps.add(app);
}
else
{
//app on phone memory
apps.add(app);
}
}
return apps;
}
最佳答案
应用安装位置在应用的 AndroidManifest.xml 中指定。看看here获取更多信息。简而言之:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="preferExternal"
... >
或
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="auto"
... >
其中 auto
表示该应用程序可以安装在外部设备上,但您没有偏好; preferExternal
表示该应用程序将尽可能安装在外部设备上
记住...
If you do not declare this attribute, your application will be installed on the internal storage only and it cannot be moved to the external storage.
所以,现在的问题是如何从应用程序中获取 androidManifest.xml 文件。那么,您可以从 APK 中获取它(如果安装后未删除)。对于那个问题,请看这篇文章:How to parse the AndroidManifest.xml file inside an .apk package
最后,在这篇文章中也找到了一个关于解析 xml 的好话题。 Accessing android:installLocation manifest attribute
干杯, 奥尔多
关于android - 我如何确定该应用程序可移动到 SD 卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10569513/