【发布时间】:2014-03-12 15:12:03
【问题描述】:
我想在带有 webview 的 Android 应用程序中使用主机 example.com 或 example.de 在浏览器中打开链接。
我创建了这个意图:
<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:
@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():
@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;
}
现在我被困住了。如果您在 Web 浏览器中单击 URL example.com/otherurl.php,应用程序将打开,但会加载默认 URL example.com。如何打开应用程序并加载 example.com/otherurl.php 而不是 example.com?
我已经读过here,我需要这段代码来获取url:
Uri data = getIntent().getData();
String extUrl = data.toString();
但是我应该在哪里实现这个代码呢? 谢谢
【问题讨论】:
标签: android android-intent webview