android - 我如何确定该应用程序可移动到 SD 卡?

标签 android

我想确定安装在手机内存上并可移动到 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/

相关文章:

java - 如何从 Java 在 Android 设备上运行 adb screenrecord 并结束屏幕录制?

android - 从另一个存储库分支克隆后推送到私有(private)存储库

java - Android:移动网站可以在 Chrome 中运行,但不能在 WebView 中运行

android - 如何使用SimpleAdapter.ViewBinder?

android - Android 中的一些下载问题

android - 将代码执行移至后台线程的阈值是多少?

android - 是否有记录在案的 Android 版本接收安全更新的生命周期?

java - SQLite 上 INNER JOIN 命令的结果表名是什么

java - 无法在 Release模式下启动 Activity(使用 proguard-android-optimize 配置)

android - notifyDataSetChanged() 不刷新适配器