android - 过滤 SD 卡/内部应用程序列表

标签 android

更新我必须以 Android 2.1 为目标。

我正在尝试找出如何以编程方式列出应用程序,并通过 SD 卡或内部存储进行过滤。

我不确定如何解决这个问题。我是否应该首先检测 SD 卡挂载点并查看其上是否有应用程序数据目录?例如

if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
  Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);     
  List<ResolveInfo> intents = m_pm.queryIntentActivities(mainIntent, 0);
  File baseSD = Environment.getExternalStorageDirectory();

  int count = intents.size();
  for (int i = 0; i < count; i++) {
    String baseApp = intents.get(i).activityInfo.applicationInfo.dataDir;
    if (baseApp.startsWith(baseSD.getAbsolutePath()) {
      // This is on the SD card, do whatever with the app details
    }
    else {
      // This is on internal storage
    }
  }
}

看起来很低调,一定有更妥当的办法。如有任何帮助,我们将不胜感激。

最佳答案

您可以使用此代码来检查应用程序的安装位置:

PackageInfo pi = pm.getPackageInfo("packageName", 0);

if ((pi.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == 0)
   ; // stored on internal storage
else 
   ; // stored on sd

请小心,因为 FLAG_EXTERNAL_STORAGE 从 Android API 8 开始可用。

关于android - 过滤 SD 卡/内部应用程序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8483240/

相关文章:

java - android 按钮可以执行另一个按钮然后执行它自己的代码吗?

java - Android:上下文菜单上的默认主题?

android - 如何在 android SQLite 数据库中存储和检索图像并将它们显示在 gridview 上

java - 当我要求它在 toast 中显示保存的数据时,我的应用程序崩溃了 - 我

android - 我怎样才能拥有带有手动控制的水平listView或gridView

android - 通过 onDraw 方法从具有动画的自定义 View 中保存位图

android - MaxWidth 和 MaxHeight 在 ImageView 中不起作用

java - 录制音频并同时播放

java - 在 Android 中使用 createScaledBitmap 创建缩放位图

android - 包含布局内的 layout_below 项