代码改变世界

Android获取手机安装的浏览器列表

  雪夜&流星  阅读(1828)  评论(0编辑  收藏  举报

最近碰到一个同事询问如何查询本地安装的浏览器列表,其使用的代码如下:

复制代码
public static List<ResolveInfo> getBrowserList(Context context) {
        PackageManager packageManager = context.getPackageManager();

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        intent.setData(Uri.parse("http://www.baidu.com/"));
        
        List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 
                       PackageManager.MATCH_DEFAULT_ONLY);
return activities; }
复制代码

运行结果如下:

com.android.browser

com.tencent.mtt

com.android.chrome

com.taobao.taobao

结果不知道怎么淘宝客户端就被当作browser添加进来了,后来发现原来淘宝客户端也添加了Intent.CATEGORY_BROWSABLE这个category过滤,但是使用

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("http://www.baidu.com/")); 
ComponentName name
= new ComponentName("android",
"com.android.internal.app.ResolverActivity");
intent.setComponent(name);
context.startActivity(intent);

这样调起系统选择的浏览器的界面就没有出现淘宝客户端,这让我感觉很意外,于是看了com.android.internal.app.ResolverActivity的代码,发现有这样一段代码:

复制代码
// Only display the first matches that are either of equal
// priority or have asked to be default options.
ResolveInfo r0 = currentResolveList.get(0);
for (int i=1; i<N; i++) {
     ResolveInfo ri = currentResolveList.get(i);
     if (r0.priority != ri.priority ||
         r0.isDefault != ri.isDefault) {
         while (i < N) {
           if (mOrigResolveList == currentResolveList) {
               mOrigResolveList = new ArrayList<ResolveInfo>(mOrigResolveList);
           }
           currentResolveList.remove(i);
           N--;
        }
     }
 }
复制代码

于是将获取浏览器列表的逻辑改成了如下:

复制代码
public static List<ResolveInfo> getBrowserList(Context context) {
        PackageManager packageManager = context.getPackageManager();

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        intent.setData(Uri.parse("http://www.baidu.com/"));
        
        List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 
                       PackageManager.MATCH_DEFAULT_ONLY); ResolveInfo r0
= activities.get(0); Iterator<ResolveInfo> activity_iter = activities.iterator(); while (activity_iter.hasNext()) { ResolveInfo resolveInfo = activity_iter.next(); if (r0.priority != resolveInfo.priority ||
          r0.isDefault != resolveInfo.isDefault) { activities.remove(resolveInfo); } } return activities; }
复制代码

运行得到的结果如下:

com.android.browser

com.tencent.mtt

com.android.chrome

 

总结:第一个是获取到的是最匹配查询条件的系统软件,所有app的优先级与其相同的即为所要获取的列表项。

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示