【问题标题】:Android: How to reload webview using a buttonAndroid:如何使用按钮重新加载 webview
【发布时间】:2014-04-13 02:56:58
【问题描述】:

我创建了一个 webview 并加载了一个 URL。

如果网络连接不可用,它会加载一个带有重试按钮的自定义 URL。

  1. 如何在用户连接网络后让此按钮重新加载此活动? (如果通过调用另一个活动然后再次调用此 webview 更容易,那么这也可以。)

  2. 我是否正确处理了网络连接不可用代码?

    下面是我的代码:

     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;
    }
    
    }
    

【问题讨论】:

  • 使用 html 在 webview 中创建一个用于重新加载的按钮
  • javascript:history.go(1) 不起作用
  • 字符串摘要 = " " ; webview.loadData(summary, "text/html", null);

标签: android webview


【解决方案1】:

在您的按钮 oncClickListener 中调用 webview.reload() 以重新加载您的网址。

【讨论】:

  • 当没有可用的网络时,它会重定向到一个自定义的 html 页面。我的按钮在一个 html 页面上,我不能调用上面的。我也希望再次调用该活动。
  • 好的。您可能会发现这篇文章很有用:stackoverflow.com/questions/5907439/…。顺便说一句,您正在以正确的方式检查连接性:D
  • 非常感谢。我会浏览那个帖子,看看它是否对我有帮助。我想我也会创建一个新帖子,更好地解释当您在没有网络连接的情况下使用 Play 商店应用程序时我想要一个界面。
【解决方案2】:

我现在已经使用以下方法解决了这个问题,我将 else 里面的代码替换为:

Intent errorIntent = new Intent(this, NetworkErrorActivity.class);
startActivity(errorIntent); 
finish();

在我的新活动中,我通过按一个按钮再次调用我以前的活动。

【讨论】:

    猜你喜欢
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 2012-05-12
    • 2015-12-20
    相关资源
    最近更新 更多