当我用一页网络视图刷新我的网络视图时,每次它都会重定向到主页 URL! 为什么?



这是我的代码......

这里有两个问题,

  1. 当我最小化此应用程序窗口并在手机上启动另一个应用程序时,再次查看此应用程序时,它不会恢复,我最小化此应用程序。

  2. 当我在此应用程序的任何页面上刷新时 Web 查看其重定向 每次都主页

我想像 youtube 一样使用进度条,但我无法执行水平条可运行。

活动主.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/root_layout"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.android.eforever.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webViewId"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="310dp"
/>
</RelativeLayout>

主要活动.java

package com.android.eforever;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private WebView webView;
private ProgressBar mProgress;
private SwipeRefreshLayout swipeRefreshLayout;
private String currentUrl="https://www.example.com";
@Override
protected void onSaveInstanceState(Bundle bundle) {
webView.saveState(bundle);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
LoadWeb(currentUrl);
}
});
LoadWeb("https://www.example.com/");
}
public void LoadWeb(String url) {
webView = (WebView) findViewById(R.id.webViewId);
mProgress = (ProgressBar) findViewById(R.id.progressBar);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webView.getSettings().setAppCacheEnabled(true);
webSettings.setEnableSmoothTransition(true);
webView.setScrollbarFadingEnabled(true);
webView.loadUrl("url");
webView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
currentUrl=url;
return super.shouldOverrideUrlLoading(view, url);
}
});
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
Toast.makeText(MainActivity.this, "Opening your default browser and then will start your download. Thank you", Toast.LENGTH_LONG).show();
}
});
webView.setWebViewClient(new CustomWebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String defaulturl = "https://www.example.com/";
// all links  with in ur site will be open inside the webview
//links that start ur domain example(https://www.aamaricche.com/)
if (url != null && url.startsWith(defaulturl)) {
return false;
}
// all links that points outside the site will be open in a normal android browse
else if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
} else if (url.startsWith("mailto:")) {
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
startActivity(intent);
return true;
} else {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");
}
public void onPageFinished(WebView view, String url) {
mProgress.setVisibility(View.GONE);
swipeRefreshLayout.setRefreshing(false);
}
});
}
private class CustomWebViewClient extends WebViewClient {
boolean onPageStarted = false;
Runnable hideLoadingRunnable = new Runnable() {
@Override
public void run() {
if (!onPageStarted) {
mProgress.setVisibility(View.GONE);
}
}
};
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
onPageStarted = true;
mProgress.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
onPageStarted = false;
webView.postDelayed(hideLoadingRunnable, 1000);
}
}
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
}

在给定的代码中发现 loadWeb(( 方法中有 2 个问题

  1. 有webView.loadUrl("url"(; 但它应该是webView.loadUrl(url(;

错误的主要原因是您在loadWeb((中两次覆盖WebViewClient

。所以最后你的loadWeb((方法应该看起来像这样。

public void LoadWeb(String url) {
webView = (WebView) findViewById(R.id.webViewId);
mProgress = (ProgressBar) findViewById(R.id.progressBar);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webView.getSettings().setAppCacheEnabled(true);
webSettings.setEnableSmoothTransition(true);
webView.setScrollbarFadingEnabled(true);
webView.loadUrl(url);
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
Toast.makeText(MainActivity.this, "Opening your default browser and then will start your download. Thank you", Toast.LENGTH_LONG).show();
}
});
webView.setWebViewClient(new CustomWebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String defaulturl = "https://www.example.com/";
currentUrl=url;
// all links  with in ur site will be open inside the webview
//links that start ur domain example(https://www.aamaricche.com/)
if (url != null && url.startsWith(defaulturl)) {
return false;
}
// all links that points outside the site will be open in a normal android browse
else if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
} else if (url.startsWith("mailto:")) {
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
startActivity(intent);
return true;
} else {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");
}
public void onPageFinished(WebView view, String url) {
mProgress.setVisibility(View.GONE);
swipeRefreshLayout.setRefreshing(false);
}
});
}

每次用户导航时保存重定向 URL 的一种方法

private String currentUrl="http://www.example.com";
webview.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
currentUrl=url;
return super.shouldOverrideUrlLoading(view, url);
}
});

最新更新