每次启动应用程序时,如何刷新Webview的内容?

用户名

我试图在每次有人启动我的应用程序时刷新Webview的内容。例如,有人进入该应用程序中的其他页面或进入后台,然后重新启动该应用程序,我希望基于我在服务器上所做的最新更改。这是我到目前为止的代码:

public class MainActivity extends Activity {
    WebView webview;
    ProgressBar progress; 
    public final static int MENU_FULLSCREEN_ON = 3;
    public final static int MENU_FULLSCREEN_OFF = 4;
    private boolean fullscreen = true;
    public static Object SPLASH_LOCK = new Object();
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
       // getWindow().requestFeature(Window.FEATURE_PROGRESS);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);
        progress = (ProgressBar) findViewById(R.id.ProgressBar);
        progress.setVisibility(View.VISIBLE);
         webview = (WebView)findViewById(R.id.webview);


         if(CheckNetwork.isInternetAvailable(MainActivity.this))
        {
        webview.setInitialScale(1);
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setLoadWithOverviewMode(true);
        webview.getSettings().setUseWideViewPort(true);
      //  webview.setWebViewClient(new MyCustomWebViewClient());
        if (Build.VERSION.SDK_INT >= 11){
            webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        webview.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        webview.setScrollbarFadingEnabled(false);
       webview.getSettings().setRenderPriority(RenderPriority.HIGH);
        webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
       // webview.getSettings().setBlockNetworkLoads(true);
        webview.getSettings().setBuiltInZoomControls(true); 
        webview.clearCache(true);
        webview.loadUrl("http://myserver.com/firstpage");
        webview.getSettings().setDomStorageEnabled(true);
        webview.getSettings().setDatabaseEnabled(true);
        webview.getSettings().setAppCacheEnabled(true);
        //http://www.inpixelitrust.fr/demos/restaurant_picker/
        //file:///android_asset/index.html
        webview.reload();
        webview.getSettings().setSupportZoom(false);
         }else{
              Toast toast = Toast.makeText(MainActivity.this, "No Internet Connection", Toast.LENGTH_LONG);
              toast.show(); 
              AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
              alertDialog.setTitle("Connection Problem");
              alertDialog.setMessage("You need to be connected to the internet to view this app");
              alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

                  public void onClick(final DialogInterface dialog, final int which) {
                      return;
                  }
              });
              alertDialog.show();

        }
       /* webview.setWebChromeClient(new WebChromeClient()
        {
         public void onProgressChanged(WebView view, int progress)
         {
           // update the progressBar
           MainActivity.this.setProgress(progress * 100);
         }
        });*/
        if (!isTaskRoot()) {
            final Intent intent = getIntent();
            final String intentAction = intent.getAction();
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
                finish();
                return;
            }
        }

        setFullscreen();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN){
            switch(keyCode)
            {
            case KeyEvent.KEYCODE_BACK:
                if(webview.canGoBack() == true){
                    webview.goBack();
                }else{
                    finish();
                }
                return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
      super.onCreateOptionsMenu(menu);

      menu.add(0, MENU_FULLSCREEN_ON, 0, R.string.menu_fullscreen_on);
      menu.add(0, MENU_FULLSCREEN_OFF, 0, R.string.menu_fullscreen_off);


      return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
      super.onPrepareOptionsMenu(menu);

      menu.findItem(MENU_FULLSCREEN_ON).setVisible(!fullscreen);
      menu.findItem(MENU_FULLSCREEN_OFF).setVisible(fullscreen);

      return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
      switch (item.getItemId()) {

      case MENU_FULLSCREEN_ON:
        fullscreen = true;
        setFullscreen();
        return true;
      case MENU_FULLSCREEN_OFF:
        fullscreen = false;
        setFullscreen();
        return true;

      }
      return false;
    }
    private void setFullscreen()
    {
      if (fullscreen) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(
            WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
      } else {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(
            WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
      }

    }
/*
    @Override
    protected void onNewIntent(Intent intent) {

          setFullscreen();


    }*/


    private class MyCustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //view.loadUrl(url);
            return true;
        }
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            progress.setVisibility(View.VISIBLE);
          }
          public void onPageFinished(WebView view, String url) {
            progress.setVisibility(View.GONE);
            synchronized (SPLASH_LOCK) {
                SPLASH_LOCK.notifyAll();
            }
          }
    }

}
克里斯·斯蒂尔威尔

onResume()当活动返回到前台时被调用。您可以在其中放置重装物品。像这样的东西:

@Override
protected void onResume(){
    super.onResume();
    webView.reload();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

每次启动应用程序时,数据库内容都会被删除

来自分类Dev

每次启动应用程序时启动活动

来自分类Dev

iOS:每次用户加载应用程序时,如何刷新MKMapView用户位置

来自分类Dev

如何停止在Android中每次启动应用程序时收到通知?

来自分类Dev

用户每次打开应用程序时如何调用函数

来自分类Dev

用户每次打开应用程序时如何调用函数

来自分类Dev

每次打开应用程序时如何调用片段?

来自分类Dev

在启动多个应用程序时如何在视图中显示不同的内容

来自分类Dev

启动Android应用程序时如何启动服务?

来自分类Dev

启动Android应用程序时如何启动服务?

来自分类Dev

使用主应用程序时如何刷新小部件?

来自分类Dev

仅在首次启动应用程序时,如何启动具有特定活动的应用程序?

来自分类Dev

如何诊断“启动应用程序时出错”?

来自分类Dev

启动应用程序时如何禁止Javers创建表

来自分类Dev

如何诊断“启动应用程序时出错”?

来自分类Dev

启动应用程序时如何分配WM_CLASS

来自分类Dev

从AndroidStudio启动应用程序时如何选择设备

来自分类Dev

从终端启动桌面应用程序时如何禁用日志

来自分类Dev

当用户打开wifi并返回应用程序时刷新webview

来自分类Dev

RestKit种子数据库是否在每次安装应用程序时刷新,还是仅在首次安装时刷新?

来自分类Dev

为什么每次重新启动应用程序时,BluetoothDevice的地址都会更改?

来自分类Dev

每次重新启动应用程序时,Spring Integration都会加载文件

来自分类Dev

每次重新启动应用程序时,NSUserDefaults中的数据均消失

来自分类Dev

每次重新启动应用程序时,我都应该调用setMinimumBackgroundFetchInterval吗?

来自分类Dev

每次启动应用程序时,在GCM上注册设备是正确的方法吗?

来自分类Dev

每次启动应用程序时资源ID都会更改吗

来自分类Dev

每次重新启动应用程序时都会丢失数据

来自分类Dev

Oracle APEX:是否在每次启动应用程序时重置会话?

来自分类Dev

每次启动应用程序时,在GCM上注册设备是正确的方法吗?

Related 相关文章

  1. 1

    每次启动应用程序时,数据库内容都会被删除

  2. 2

    每次启动应用程序时启动活动

  3. 3

    iOS:每次用户加载应用程序时,如何刷新MKMapView用户位置

  4. 4

    如何停止在Android中每次启动应用程序时收到通知?

  5. 5

    用户每次打开应用程序时如何调用函数

  6. 6

    用户每次打开应用程序时如何调用函数

  7. 7

    每次打开应用程序时如何调用片段?

  8. 8

    在启动多个应用程序时如何在视图中显示不同的内容

  9. 9

    启动Android应用程序时如何启动服务?

  10. 10

    启动Android应用程序时如何启动服务?

  11. 11

    使用主应用程序时如何刷新小部件?

  12. 12

    仅在首次启动应用程序时,如何启动具有特定活动的应用程序?

  13. 13

    如何诊断“启动应用程序时出错”?

  14. 14

    启动应用程序时如何禁止Javers创建表

  15. 15

    如何诊断“启动应用程序时出错”?

  16. 16

    启动应用程序时如何分配WM_CLASS

  17. 17

    从AndroidStudio启动应用程序时如何选择设备

  18. 18

    从终端启动桌面应用程序时如何禁用日志

  19. 19

    当用户打开wifi并返回应用程序时刷新webview

  20. 20

    RestKit种子数据库是否在每次安装应用程序时刷新,还是仅在首次安装时刷新?

  21. 21

    为什么每次重新启动应用程序时,BluetoothDevice的地址都会更改?

  22. 22

    每次重新启动应用程序时,Spring Integration都会加载文件

  23. 23

    每次重新启动应用程序时,NSUserDefaults中的数据均消失

  24. 24

    每次重新启动应用程序时,我都应该调用setMinimumBackgroundFetchInterval吗?

  25. 25

    每次启动应用程序时,在GCM上注册设备是正确的方法吗?

  26. 26

    每次启动应用程序时资源ID都会更改吗

  27. 27

    每次重新启动应用程序时都会丢失数据

  28. 28

    Oracle APEX:是否在每次启动应用程序时重置会话?

  29. 29

    每次启动应用程序时,在GCM上注册设备是正确的方法吗?

热门标签

归档