代码之家  ›  专栏  ›  技术社区  ›  the_void

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

  •  9
  • the_void  · 技术社区  · 14 年前

    我想专门为给定的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 }
    

    我也试着补充 <uses-library android:name="com.google.android.browser" /> 到舱单上。

    我这里有什么东西不见了吗?

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

    3 回复  |  直到 14 年前
        1
  •  6
  •   Janusz Daniel Rindt    14 年前

    我用这个,没关系。

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

    我想你知道怎么了。:)

        2
  •  6
  •   Pentium10    14 年前

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

    你需要这样做:

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri data = Uri.parse("http://www.google.com");
    intent.setData(data);
    startActivity(intent);
    
        3
  •  1
  •   Janusz Daniel Rindt    14 年前

    在浏览器中从代码打开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浏览器。

    希望这有帮助…:)