在Android Webview中从浏览器打开链接
我想在带有Webview的Android应用程序中打开主机为example.com
或example.de
的浏览器中的链接.
我创建了这个意图:
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>
默认情况下,WebView加载URL example.com
,这是我的onCreate:
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);
}
这是我的shouldOverrideUrlLoading():
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;
}
现在,我被卡住了.如果您在网络浏览器中单击URL example.com/otherurl.php
,则该应用程序将打开,但会加载默认的URL example.com
.如何打开应用程序并加载example.com/otherurl.php
而不是example.com
?
我已经阅读了此处,我需要这段代码来获取网址:
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
extUrl将包含要打开的URL.因此,应该在调用loadUrl()的地方执行类似的操作:
extUrl will contain the url to open. So you should do something like this where you called loadUrl():
Uri data = getIntent().getData();
if (data != null) {
mWebView.loadUrl(data.toString());
} else {
mWebView.loadUrl(url);
}
因此,您在这里所说的是,如果浏览器向您发送了url,则数据不会为空,因此请使用该地址启动webview.否则,如果数据不包含地址且为null,请使用默认地址(url变量)启动.
So what you're saying here is that if the browser sent you the url, data won't be null so launch the webview with that address. Otherwise, if data does not contain an address and is null, launch with your default address (the url variable).