0

I've created an App in Android Studio which is running a web application. In that application there are some links that I wish to open from Chrome not within in the webview of the application.

I've added the link that I have reviewed on here already and tried to add in to my code, but currently the links still open within my app not in Chrome, am I missing something obvious please? Thank you.

Article where I added the code from

WebView link click open default browser

Code for my app:

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();
}

}

Community
  • 1
  • 1
Brian
  • 11
  • 5

1 Answers1

1

By package

 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);
    }

By scheme

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
}

Or:

 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
    }
Luiz Fernando Salvaterra
  • 4,192
  • 2
  • 24
  • 42