I would like to open links in browsers with the host example.com
or example.de
in my Android application with webview.
I created this intent:
<intent-filter>
<data android:scheme="https" android:host="example.com" />
<data android:scheme="https" android:host="example.de" />
<data android:scheme="http" android:host="example.com" />
<data android:scheme="http" android:host="example.de" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
By default, the WebView loads the URL example.com
, here is my onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...", true);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
pd.show();
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
if (pd.isShowing() && pd != null) {
pd.dismiss();
}
}
});
mWebView.loadUrl(url);
}
This is my shouldOverrideUrlLoading():
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("example.com") || Uri.parse(url).getHost().endsWith("example.de") ) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
Now I am stuck. If you click on the URL example.com/otherurl.php
in a web browser, the app opens, but loads the default url example.com
. How can I open the app and load example.com/otherurl.php
instead of example.com
?
I already read here, that I need this piece of code to get the url:
Uri data = getIntent().getData();
String extUrl = data.toString();
But where should I implement this code? Thanks