更新我必须以 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/