如何从我的应用程序在 Android 的网络浏览器中打开 URL?
如何从内置 Web 浏览器中的代码而不是在我的应用程序中打开 URL?
我尝试过这个:
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();
}
但我遇到了异常:
No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com
How to open a 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
试试这个:
这对我来说效果很好。
至于缺少的“http://”,我只会做这样的事情:
我也可能会预先填充用户在其中输入“http://”的 URL 的 EditText。
在科特林中:
Try this:
That works fine for me.
As for the missing "http://" I'd just do something like this:
I would also probably pre-populate your EditText that the user is typing a URL in with "http://".
In Kotlin:
实现此目的的常见方法是使用下一个代码:
可以将其更改为短代码版本......
或者:
最短! :
A common way to achieve this is with the next code:
that could be changed to a short code version ...
or :
the shortest! :
简单答案
您可以查看Android Developer 的官方示例。
它是如何工作的
请查看
Intent 的构造函数
:您可以将
android.net.Uri
实例传递给第二个参数,并根据给定的数据 url 创建一个新的 Intent。然后,只需调用
startActivity(Intent Intent)
即可启动一个新的 Activity,该 Activity 与具有给定 URL 的 Intent 捆绑在一起。我需要
if
检查语句吗?是的。 docs 说:
额外的好处
你可以在创建 Intent 实例时写一行,如下所示:
Simple Answer
You can see the official sample from Android Developer.
How it works
Please have a look at the constructor of
Intent
:You can pass
android.net.Uri
instance to the 2nd parameter, and a new Intent is created based on the given data url.And then, simply call
startActivity(Intent intent)
to start a new Activity, which is bundled with the Intent with the given URL.Do I need the
if
check statement?Yes. The docs says:
Bonus
You can write in one line when creating the Intent instance like below:
在 2.3 中,我的运气更好,
区别在于使用
Intent.ACTION_VIEW
而不是字符串"android.intent.action.VIEW"
In 2.3, I had better luck with
The difference being the use of
Intent.ACTION_VIEW
rather than the String"android.intent.action.VIEW"
Kotlin 答案:
我在
Uri
上添加了一个扩展,使这变得更加容易作为奖励,这里有一个简单的扩展函数,可以安全地将字符串转换为 Uri。
The Kotlin answer:
I have added an extension on
Uri
to make this even easierAs a bonus, here is a simple extension function to safely convert a String to Uri.
试试这个:
或者如果你想在你的活动中打开网络浏览器,然后这样做:
如果你想在浏览器中使用缩放控制,那么你可以使用:
Try this:
or if you want then web browser open in your activity then do like this:
and if you want to use zoom control in your browser then you can use:
如果您想向用户显示与所有浏览器列表的对话,以便他可以选择首选,这里是示例代码:
If you want to show user a dialogue with all browser list, so he can choose preferred, here is sample code:
就像其他人写的解决方案(工作正常)一样,我想回答同样的问题,但有一个我认为大多数人更喜欢使用的提示。
如果您希望在新任务中开始打开应用程序,独立于您自己的应用程序,而不是停留在同一堆栈上,您可以使用以下代码:
还有一种方法可以在 Chrome 自定义标签 。 Kotlin 中的示例:
Just like the solutions other have written (that work fine), I would like to answer the same thing, but with a tip that I think most would prefer to use.
In case you wish the app you start to open in a new task, indepandant of your own, instead of staying on the same stack, you can use this code:
There is also a way to open the URL in Chrome Custom Tabs . Example in Kotlin :
使用 Webview 在同一应用程序中加载 URL 中的其他选项
other option In Load Url in Same Application using Webview
您也可以这样
在 xml 中:
在 java 代码中:
在 Manifest 中不要忘记添加互联网权限...
You can also go this way
In xml :
In java code :
In Manifest dont forget to add internet permission...
所以我已经找了很长时间了,因为所有其他答案都是打开该链接的默认应用程序,而不是默认浏览器,这就是我想要的。
我终于成功做到了:
顺便说一句,您可以注意到
context
。无论如何,因为我已将其用于静态 util 方法,如果您在活动中执行此操作,则不需要它。So I've looked for this for a long time because all the other answers were opening default app for that link, but not default browser and that's what I wanted.
I finally managed to do so:
BTW, you can notice
context
.whatever, because I've used this for a static util method, if you are doing this in an activity, it's not needed.Webview 可用于在应用程序中加载 Url。
URL 可以由用户在文本视图中提供,也可以对其进行硬编码。
另外不要忘记 AndroidManifest.xml 中的互联网权限。
Webview can be used to load Url in your applicaion.
URL can be provided from user in text view or you can hardcode it.
Also don't forget internet permissions in AndroidManifest.
在您的 try 块中,粘贴以下代码,Android Intent 直接使用 URI(统一资源标识符)大括号内的链接来识别链接的位置。
你可以试试这个:
Within in your try block,paste the following code,Android Intent uses directly the link within the URI(Uniform Resource Identifier) braces in order to identify the location of your link.
You can try this:
短代码版本...
A short code version...
简单且最佳实践
方法 1:
方法 2:
Simple and Best Practice
Method 1:
Method 2:
短&可爱的 Kotlin 辅助函数:
Short & sweet Kotlin helper function:
Chrome 自定义选项卡现已可用:
第一步是将自定义选项卡支持库添加到您的 build.gradle 文件中:
然后,打开 Chrome 自定义选项卡:
了解更多信息:
https://developer.chrome.com/multidevice/android/customtabs
Chrome custom tabs are now available:
The first step is adding the Custom Tabs Support Library to your build.gradle file:
And then, to open a chrome custom tab:
For more info:
https://developer.chrome.com/multidevice/android/customtabs
简单,通过意图查看网站,
使用这个简单的代码在 Android 应用程序中查看您的网站。
在清单文件中添加互联网权限,
Simple, website view via intent,
use this simple code toview your website in android app.
Add internet permission in manifest file,
在 Android 11 中从 URL 打开链接的一种新的更好方法。
参考:
https://medium.com/androiddevelopers/package-visibility-in-android-11-cc857f221cd9 和 https://developer.android.com/training/package-visibility/use-cases#avoid-a-disambiguation-dialog
A new and better way to open link from URL in Android 11.
References:
https://medium.com/androiddevelopers/package-visibility-in-android-11-cc857f221cd9 and https://developer.android.com/training/package-visibility/use-cases#avoid-a-disambiguation-dialog
MarkB 的回应是正确的。就我而言,我使用的是 Xamarin,与 C# 和 Xamarin 一起使用的代码是:
此信息取自: https://developer.xamarin.com/recipes/android/fundamentals/intent/open_a_webpage_in_the_browser_application/
The response of MarkB is right. In my case I'm using Xamarin, and the code to use with C# and Xamarin is:
This information is taked from: https://developer.xamarin.com/recipes/android/fundamentals/intent/open_a_webpage_in_the_browser_application/
基于 Mark B 的回答和以下评论:
Based on the answer by Mark B and the comments bellow:
这种方式使用了一种方法,允许您输入任何字符串,而不是固定输入。如果重复使用多次,这确实可以节省一些代码行,因为您只需要三行来调用该方法。
使用这种方法使其具有普遍用途。 IT 不必放置在特定的活动中,因为您可以像这样使用它:
或者如果您想在活动之外启动它,您只需在活动实例上调用 startActivity 即可:
如这两个代码块所示是一个空检查。这是因为如果没有应用程序来处理意图,它会返回 null。
如果没有定义协议,此方法默认为 HTTP,因为有些网站没有 SSL 证书(HTTPS 连接所需的证书),如果您尝试使用 HTTPS 但不存在,这些网站将停止工作。任何网站仍然可以强制转换为 HTTPS,因此无论哪种方式,这些网站都会让您进入 HTTPS。
因为此方法使用外部资源来显示页面,所以您无需声明 Internet 权限。显示网页的应用程序必须执行此操作
This way uses a method, to allow you to input any String instead of having a fixed input. This does save some lines of code if used a repeated amount of times, as you only need three lines to call the method.
Using this method makes it universally usable. IT doesn't have to be placed in a specific activity, as you can use it like this:
Or if you want to start it outside an activity, you simply call startActivity on the activity instance:
As seen in both of these code blocks there is a null-check. This is as it returns null if there is no app to handle the intent.
This method defaults to HTTP if there is no protocol defined, as there are websites who don't have an SSL certificate(what you need for an HTTPS connection) and those will stop working if you attempt to use HTTPS and it isn't there. Any website can still force over to HTTPS, so those sides lands you at HTTPS either way
Because this method uses outside resources to display the page, there is no need for you to declare the INternet permission. The app that displays the webpage has to do that
android.webkit.URLUtil
具有方法guessUrl(String)
工作得很好(即使使用file://
或data ://
)自Api 级别 1
(Android 1.0) 起。用作:在 Activity 调用中:
检查完整的
guessUrl
代码 了解更多信息。android.webkit.URLUtil
has the methodguessUrl(String)
working perfectly fine (even withfile://
ordata://
) sinceApi level 1
(Android 1.0). Use as:In the Activity call:
Check the complete
guessUrl
code for more info.该错误是由于 URL 无效而发生的,Android 操作系统无法找到您的数据的操作视图。所以您必须验证 URL 是否有效。
That error occurred because of invalid URL, Android OS can't find action view for your data. So you have validate that the URL is valid or not.
科特林
Kotlin
我检查了每个答案,但是哪个应用程序具有与用户想要使用的相同 URL 的深度链接?
今天我收到这个案例,答案是
browserIntent.setPackage("browser_package_name");
例如:
I checked every answer but what app has deeplinking with same URL that user want to use?
Today I got this case and answer is
browserIntent.setPackage("browser_package_name");
e.g. :
Jetpack Compose
如果您使用 jetpack compose,则需要首先获取可组合函数的上下文
Jetpack Compose
If you are using jetpack compose, you need to get the context of your composable function first
我认为这是最好的
将下面的代码放入全局类中
I think this is the best
Put below code into global class