使用自定义类启动浏览器意图 - 找不到 Activity

发布于 2024-09-11 03:59:07 字数 1166 浏览 3 评论 0原文

我想专门为给定的 URL 运行默认的 Android 浏览器。我正在使用此代码:

Intent i = new Intent();
i.setAction("android.intent.action.VIEW"); 
i.addCategory("android.intent.category.BROWSABLE");
i.setClassName("com.google.android.browser", "com.android.browser.BrowserActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setData(Uri.parse(url));
startActivity(i);

我收到的错误是:

Unable to find explicit activity class {
com.google.android.browser/com.android.browser.BrowserActivity}; 
have you declared this activity in your AndroidManifest.xml?

我还尝试按包过滤意图:

i.setPackage("com.google.android.browser");

而不是 setClassName,但无济于事:

No Activity found to handle Intent { act=android.intent.action.VIEW 
cat=[android.intent.category.BROWSABLE] 
dat=http://www.google.com/ flg=0x10000000 pkg=android }

我还尝试添加 到清单。

我在这里错过了什么吗?

PS:我对使用 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"))) 不感兴趣,因为它会列出所有选择用于浏览意图

I want to specifically run the default Android browser for a given URL. I'm using this code:

Intent i = new Intent();
i.setAction("android.intent.action.VIEW"); 
i.addCategory("android.intent.category.BROWSABLE");
i.setClassName("com.google.android.browser", "com.android.browser.BrowserActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setData(Uri.parse(url));
startActivity(i);

The error I receive is:

Unable to find explicit activity class {
com.google.android.browser/com.android.browser.BrowserActivity}; 
have you declared this activity in your AndroidManifest.xml?

I also tried filtering the intents by the package:

i.setPackage("com.google.android.browser");

instead of setClassName, but to no avail:

No Activity found to handle Intent { act=android.intent.action.VIEW 
cat=[android.intent.category.BROWSABLE] 
dat=http://www.google.com/ flg=0x10000000 pkg=android }

I also tried adding <uses-library android:name="com.google.android.browser" /> to the manifest.

Am I missing something here?

PS: I'm not interested in using startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"))) as it will list all the choices for the browsing Intent.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海里的那抹蓝 2024-09-18 03:59:07

我用这个,没问题

intent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));

我想你知道出了什么问题。 :)

i use this, it's ok.

intent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));

i think you know what wrong. :)

半透明的墙 2024-09-18 03:59:07

请注意,默认浏览器可以被覆盖,并且它并不总是内置的浏览器应用程序,它可以是例如 Opera Mini。

你需要这样做:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("http://www.google.com");
intent.setData(data);
startActivity(intent);

Please note the default browser can be overridden and it's not always the built in browser app, it can be for example Opera Mini.

You need to do this way:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("http://www.google.com");
intent.setData(data);
startActivity(intent);
有木有妳兜一样 2024-09-18 03:59:07

从代码在浏览器中打开 URL 的一种方法是使用 webview 。

创建一个扩展 WebViewClient 的 WebViewClientClass,如下所示:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
   view.loadUrl(url);
   return true;
}

然后创建一个 webview。
Set webview.setWebViewClient(new WebViewClientClass()); --- 这是一个小解决方法,以便默认 Web 浏览器不会接管。

然后获取 edittext 中的 url 并将其设置为加载浏览器:

webview.loadurl(urlfield.getText().toString());
webview.requestFocus();

这应该使用您请求的 URL 加载 Web 浏览器。

希望这有帮助...:)

One way to open an URL in a browser from code is to use a webview .

Create a WebViewClientClass that extends the WebViewClient like :

public boolean shouldOverrideUrlLoading(WebView view, String url) {
   view.loadUrl(url);
   return true;
}

Then create a webview.
Set webview.setWebViewClient(new WebViewClientClass()); --- this is a small workaround so that the default web browser doesn't take over .

Then take the url in an edittext and set it to load the browser as :

webview.loadurl(urlfield.getText().toString());
webview.requestFocus();

This should load the web browser with the URL that you requested.

Hope this helps... :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文