如何每5秒刷新一次Android WebView活动?

时间:2018-09-08 05:27:49

标签: android android-studio webview

我有一个Android应用程序,我想每5分钟刷新一次我的网站的屏幕。有人可以帮忙吗?

这是我的代码

public class MainActivity extends Activity {
    private WebView mWebView;
    static {
        System.loadLibrary("native-lib");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.loadUrl("https://google.com/");
    }

    public native String stringFromJNI();
}

3 个答案:

答案 0 :(得分:1)

尝试一下
 解决方案1:

Timer timer = new Timer ();

TimerTask hourlyTask = new TimerTask () {
@Override
public void run () 
{
    mWebView.loadUrl("https://google.com/");
}};
timer.schedule (hourlyTask, 0l, 5000*60*60);

解决方案2:

public void reloadWebView() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
         mWebview.loadUrl("http://www.google.com");
    }
}, 50000);}  

答案 1 :(得分:0)

调用reload()方法onCreate()

public void reload() {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something after 5min = 50000ms
                Toast.makeText(LogThirdPager.this, "Hello", Toast.LENGTH_SHORT).show();
                reload();
                // mWebview.loadUrl("http://www.google.com");
            }
        }, 50000);
    }

答案 2 :(得分:0)

您可以每5分钟使用 TimerTask ,如下所示”

Timer timer=new Timer();
TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
         if(webView!=null)
            reload();   // reload your webview
        }
    };
timer.scheduleAtFixedRate(timerTask,0,300000); // 300000 ms = 5 minutes