【问题标题】:Trying to open a link for an external URL open in Chrome from inside an Android App尝试从 Android 应用程序内部打开在 Chrome 中打开的外部 URL 的链接
【发布时间】:2017-04-20 16:04:47
【问题描述】:

我在 Android Studio 中创建了一个运行 Web 应用程序的应用程序。在该应用程序中,我希望从 Chrome 打开一些链接,而不是在应用程序的 web 视图中。

我已经在这里添加了我已经查看过的链接并尝试添加到我的代码中,但目前这些链接仍然在我的应用程序中而不是在 Chrome 中打开,请问我遗漏了一些明显的东西吗?谢谢你。

我在其中添加代码的文章

WebView link click open default browser

我的应用程序代码:

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

WebView tpappview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setPage();
}

private void setPage(){

    tpappview = (WebView) findViewById(R.id.tpViewId);
    WebSettings tpsetting =tpappview.getSettings();
    tpsetting.setJavaScriptEnabled(true);
    tpappview.loadUrl("http://example.com/Login");
    tpappview.setWebViewClient(new WebViewClient());
}

private class MyWebViewClient extends WebViewClient {
    @SuppressWarnings("deprecation")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().contains   
("http://example.com")) {
            // This is my web site, so do not override; let my WebView load 
the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch 
another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

@Override
public void onBackPressed() {

    if (tpappview.canGoBack())
        tpappview.goBack();
    else
    super.onBackPressed();
}

}

【问题讨论】:

    标签: android


    【解决方案1】:

    按包装

     String url = "http://www.example.com";
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setPackage("com.android.chrome");
        try {
            startActivity(i);
        } catch (ActivityNotFoundException e) {
            // Chrome is probably not installed
            // Try with the default browser
            i.setPackage(null);
            startActivity(i);
        }
    

    按计划

    String url = "http://www.example.com";
    try {
        Uri uri = Uri.parse("googlechrome://navigate?url=" + url);
        Intent i = new Intent(Intent.ACTION_VIEW, uri);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
    } catch (ActivityNotFoundException e) {
        // Chrome is probably not installed
    }
    

    或者:

     String url = "http://www.example.com";
        try {
            Intent i = new Intent("android.intent.action.MAIN");
            i.setComponent(ComponentName.unflattenFromString("com.android.chrome/com.android.chrome.Main"));
            i.addCategory("android.intent.category.LAUNCHER");
            i.setData(Uri.parse(url));
            startActivity(i);
        }
        catch(ActivityNotFoundException e) {
            // Chrome is probably not installed
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多