以编程方式为活动设置默认浏览器 [英] Setting default browser for activity programmatically

查看:38
本文介绍了以编程方式为活动设置默认浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 android 应用程序,除其他功能外,我需要在外部网络浏览器中打开一些网址.我能否以编程方式为此设置默认应用程序,以便用户无法从可用浏览器列表中进行选择?我的意思是,我只想为我的应用程序设置默认浏览器,而不是为整个操作系统设置.

I'm developing an android app and among other functionality I need to open some urls in external web browser. Can I programmatically set a default application for that, so the user won't be able to choose from the list of available browsers? I mean, I want to set default browser only for my app but not for the whole operating system.

推荐答案

是的,为此您可以强制您的应用程序始终只打开原生 android 浏览器.为此,您必须确定 Browser 应用程序的启动 Activity,如下所示:

Yes, for this you can force your application to always open native android browser only. For this you have to identify the launching Activity of Browser application, something like this:

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.google.android.browser","com.google.android.browser.BrowserActivity"));
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.BROWSABLE");
Uri uri = Uri.parse(url);
intent.setData(uri);
try
{
    startActivity(intent);
}
catch (Exception e)
{
   e.printStackTrace();
}

这篇关于以编程方式为活动设置默认浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆