我怎样才能从我的应用程序中打开在Android的Web浏览器的URL? [英] How can I open a URL in Android's web browser from my application?

查看:206
本文介绍了我怎样才能从我的应用程序中打开在Android的Web浏览器的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从code在内置的网页浏览器,而不是在我的应用程序中打开一个网址?

我尝试这样做:

 尝试{
    意图myIntent =新的意图(Intent.ACTION_VIEW,Uri.parse(download_link));
    startActivity(myIntent);
}赶上(ActivityNotFoundException E){
    Toast.makeText(这一点,没有应用程序可以处理这个请求。
        +请安装一个网页浏览器,Toast.LENGTH_LONG).show();
    e.printStackTrace();
}
 

但我得到了一个例外:

 无活动处理意向{行动= android.intent.action.VIEW数据= www.google.com
 

解决方案

试试这个:

 意图browserIntent =新的意图(Intent.ACTION_VIEW,Uri.parse(http://www.google.com));
startActivity(browserIntent);
 

这对我来说工作得很好。

至于失踪的http://我只是做这样的事情:

 如果(!url.startsWith(HTTP://)及和放大器; url.startsWith(https://开头!))
   URL =HTTP://+网址;
 

我也大概pre-填充您的EditText的用户键入一个网址以http://。

How to open an URL from code in the built-in web browser rather than within my application?

I tried this:

try {
    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(download_link));
    startActivity(myIntent);
} catch (ActivityNotFoundException e) {
    Toast.makeText(this, "No application can handle this request."
        + " Please install a webbrowser",  Toast.LENGTH_LONG).show();
    e.printStackTrace();
}

but I got an Exception:

No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com

解决方案

Try this:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);

That works fine for me.

As for the missing "http://" I'd just do something like this:

if (!url.startsWith("http://") && !url.startsWith("https://"))
   url = "http://" + url;

I would also probably pre-populate your EditText that the user is typing a URL in with "http://".

这篇关于我怎样才能从我的应用程序中打开在Android的Web浏览器的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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