英文:
URL Automatic redirecting in WebView - Android
问题
以下是翻译好的内容:
在 XML 中:
<WebView
android:layout_alignParentTop="true"
android:id="@+id/campaign_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
在 Activity 中:
campaign_webview.getSettings().setJavaScriptEnabled(true);
campaign_webview.getSettings().setLoadWithOverviewMode(true);
campaign_webview.getSettings().setDomStorageEnabled(true);
campaign_webview.getSettings().setUseWideViewPort(true);
campaign_webview.getSettings().setBuiltInZoomControls(false);
campaign_webview.getSettings().setPluginState(WebSettings.PluginState.ON);
campaign_webview.setWebViewClient(new CustomClient());
campaign_webview.loadUrl(url);
private class CustomClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
Log.d("web_view: finish", url);
}
}
英文:
My WebView in my app are working fine, but when i load the URL= https://www.esheba.cnsbd.com/#/
Its automatically Redirecting to a Play store Link which is linked as download link in bottom of the website, But when i brows the link form browser is working fine. Please anyone tell me why its redirecting only in WebView ?
here is my codes are using.
In XML
<WebView
android:layout_alignParentTop="true"
android:id="@+id/campaign_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
In Activity
campaign_webview.getSettings().setJavaScriptEnabled(true);
campaign_webview.getSettings().setLoadWithOverviewMode(true);
campaign_webview.getSettings().setDomStorageEnabled(true);
campaign_webview.getSettings().setUseWideViewPort(true);
campaign_webview.getSettings().setBuiltInZoomControls(false);
campaign_webview.getSettings().setPluginState(WebSettings.PluginState.ON);
campaign_webview.setWebViewClient( new CustomClient());
campaign_webview.loadUrl(url);
private class CustomClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
Log.d("web_view: finish",url);
}
}
答案1
得分: 1
我认为您想在链接为Play Store链接时进行重定向到Play Store。所以,如果这是您想要的内容,您可以使用以下代码:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("intent://")) {
//加载商店
try {
Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
PackageManager packageManager = context.getPackageManager();
ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (info != null) {
context.startActivity(intent);
}
return true;
} catch (ActivityNotFoundException e) {
//如果未找到活动,请尝试加载它并显示一个toast。
Toast.makeText(context, "Activity not found", Toast.LENGTH_SHORT).show();
return false;
}
} else {
//加载URL
view.loadUrl(url);
return false;
}
}
英文:
I think you want to redirect to Play Store if it is a Play Store link. So if that is what you want then you can use:
@Override
public boolean shouldOverrideUrlLoading(WebView
view, String url) {
if (url.startsWith("intent://")) {
//load store
try{
Intent intent = Intent.parseUri(url,
Intent.URI_INTENT_SCHEME);
PackageManager packageManager =
context.getPackageManager();
ResolveInfo info =
packageManager.resolveActivity(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (info != null) {
context.startActivity(intent);
}
return true;
}catch((ActivityNotFoundException e)
{
//If activity not found try to load it and also a display toast.
Toast.makeText(context, "Activity not found", Toast.LENGTH_SHORT).show();
return false;
}
}
else
{
//load url
view.loadUrl(url);
return false;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论