android - ResolveInfo.isDefault 始终为 false

标签 android

我正在尝试获取与扩展关联的默认程序的图标。

这是我的代码:

Intent intent = new Intent(Intent.ACTION_VIEW);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String tt = mime.getMimeTypeFromExtension(getExtension());

intent.setDataAndType(Uri.fromFile(getFile()), tt);

List<ResolveInfo> matches = c.getPackageManager().queryIntentActivities(intent, 0);

for (ResolveInfo match : matches) {
     if(match.isDefault){
          //GET ICON
     }
}

问题是 match.isDefault 总是返回 false,即使我尝试将 PackageManager 的标志从 0 设置为 PackageManager.MATCH_DEFAULT_ONLY.

显然,我正在测试的文件(视频)默认与程序(MX Player)相关联。

有人可以帮帮我吗?

提前致谢。

最佳答案

作为替代解决方案,您可能希望使用此方法获取默认 Intent :

     ResolveInfo info = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);

返回的结果是:

  1. 如果没有为 Intent 设置默认值: info.activityInfo.name 等于“com.android.internal.app.ResolverActivity”

  2. 如果为 Intent 设置了任何默认应用程序,则您可以检查 ResolveInfo 对象以获取默认应用程序信息。

编辑:

对于url,你可以这样做:

    //Example: youtube url
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=something"));
    ResolveInfo defaultResolution = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);

    //Print the activity name
    Log.i(TAG, "defaultResolution:" + defaultResolution.activityInfo.name);

关于android - ResolveInfo.isDefault 始终为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11894952/

相关文章:

android - 如何将 android PublicKey 转换为字符串并返回?

android - 使用 YouTube Android 播放器 api 1.2.2 时发生 OutOfMemoryError

java - 错误: package android. util不存在

android - DiffUtil 没有更新 recyclerview

java - 如何在 Android 中的操作栏下方添加阴影?

android - 存储访问框架持久权限不起作用

android - DllImport - DllNotFoundException 与 android .so

android - Activity 中的线程是否在启动另一个时终止?

android - 在表面 View 中授予权限后相机未打开

java - Android中获取函数值