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

the*_*oid 9 android android-emulator android-intent android-browser

我想专门为给定的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);
Run Code Online (Sandbox Code Playgroud)

我收到的错误是:

Unable to find explicit activity class {
com.google.android.browser/com.android.browser.BrowserActivity}; 
have you declared this activity in your AndroidManifest.xml?
Run Code Online (Sandbox Code Playgroud)

我也尝试过包过滤意图:

i.setPackage("com.google.android.browser");
Run Code Online (Sandbox Code Playgroud)

而不是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 }
Run Code Online (Sandbox Code Playgroud)

我也尝试过添加<uses-library android:name="com.google.android.browser" />清单.

我在这里错过了什么吗?

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

Pen*_*m10 6

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

你需要这样做:

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

  • 正如我在我的问题中所述,我希望能够运行*默认的Android浏览器*,而不是*默认选择的*浏览器.作为一个副节点,我首先尝试使用你提到的解决方案,但无济于事,因为它显然启动了设置为默认的浏览器(或启动应用程序选择器). (4认同)

小智 6

我用它,没关系.

intent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
Run Code Online (Sandbox Code Playgroud)

我想你知道什么是错的.:)