beowulf
beowulf

Reputation: 556

How to open Urls in the phone and get my app as choice with other browsers?

I already have a web view activity but I don't know how to add it to the other browsers as option to handle the url? like on the picture below. I was searching already here but I did not manage to find any answer which would help me. Thanks

Upvotes: 1

Views: 75

Answers (2)

Milad Nouri
Milad Nouri

Reputation: 1597

add url schemes as intent filters to your activity in manifest. for example to handle http://yoursite.com/path/etc... use this:

<activity
                    android:name="com.app.BrowserActivity" >
                    <intent-filter>
                      <action android:name="android.intent.action.VIEW" >

                      <category android:name="android.intent.category.DEFAULT" />
                      <category android:name="android.intent.category.BROWSABLE" />
                      <data
                            android:host="yoursite.com"
                            android:pathPattern="/path/..*"
                            android:scheme="http" />

                    </intent-filter>
    </activity>

and in your activity get url:

    // get url
    final Intent intent = getIntent();

    String url = intent.getDataString();

Upvotes: 1

Bruce Lan
Bruce Lan

Reputation: 357

Use the code when you want to open url with a web browser as follow:

Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
startActivity(browserIntent);

If you want to open the link of your WebView, you need to implement the WebViewClient code as follow:

WebView webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new MyWebViewClient());
String url = "http://javatechig.com";
            webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);   

implement the WebViewClient

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // use the url to open other web browser
    Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
        startActivity(browserIntent);

        // don't load the url by your webview
        //view.loadUrl(url);
        return true;
    }
}

Upvotes: 0

Related Questions