Android:如何使用按钮重新加载webview
我已经创建了一个webview并加载了一个URL 如果网络连接不可用,它将加载带有重试按钮的自定义URLAndroid:如何使用按钮重新加载webview,android,webview,Android,Webview,我已经创建了一个webview并加载了一个URL 如果网络连接不可用,它将加载带有重试按钮的自定义URL 用户连接网络后,如何让此按钮重新加载此活动?(如果调用另一个活动然后再次调用此webview更容易,那么也可以这样做。) 我是否正确处理了网络连接不可用的代码 下面是我的代码: import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; imp
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
public class WebActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
if(isNetworkStatusAvailable (getApplicationContext())) {
Toast.makeText(getApplicationContext(), "Loading...", Toast.LENGTH_SHORT).show();
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.bing.com");
myWebView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(getApplicationContext(), "Internet Connection Unavailable Or " + description , Toast.LENGTH_LONG).show();
view.loadUrl("file:///android_asset/mob.html");
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
} else {
Toast.makeText(getApplicationContext(), "Internet Connection Unavailable.", Toast.LENGTH_LONG).show();
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("file:///android_asset/mob.html");
}
// Show the Up button in the action bar.
setupActionBar();
}
public static boolean isNetworkStatusAvailable (Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null)
{
NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
if(netInfos != null)
if(netInfos.isConnected())
return true;
}
return false;
}
}
在按钮OnClickListener中调用webview.reload()以重新加载url 我现在使用以下方法解决了这个问题,我将else中的代码替换为:
Intent errorIntent = new Intent(this, NetworkErrorActivity.class);
startActivity(errorIntent);
finish();
在我的新活动中,我通过按下按钮再次调用上一个活动。使用html在webview中创建一个按钮以重新加载JavaScript:history.go(1)不工作字符串summary=“Reload”;loadData(摘要,“text/html”,空);当没有可用的网络时,它会重定向到自定义html页面。我的按钮在html页面上,我不能调用上面的按钮。另外,我期待着再次调用该活动。好的。你可能会发现这篇文章很有用:。顺便说一句,您检查连接的方法是正确的:数据非常感谢。我会浏览那篇文章,看看它是否对我有帮助。我想我会写一篇新帖子,更好地解释当你在没有网络连接的情况下使用play store应用程序时,我是如何想要一个界面的。