每次启动应用程序时如何刷新 webview 的内容?
How do I refresh the contents of a webview everytime the app is launched?
每当有人启动我的 app.Say 有人转到应用程序中的不同页面或转到后台然后重新启动应用程序时,我都试图刷新我的 webview 的内容,我想要我的 webview 的内容到 refresh/reload 基于我在我的服务器上所做的最新更改。
这是我目前的代码:
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()
是在activityreturns到前台的时候调用的。你可以把你的重载放在那里。像这样:
@Override
protected void onResume(){
super.onResume();
webView.reload();
}
您可以这样做,但请确保检查 WebView
不为空,因为 onResume
在 onCreate
之前被调用,而 WebView
可能不会还存在 onResume
!
@Override
protected void onResume() {
super.onResume();
if(webView != null){
webView.loadUrl(...);
or
webView.reload();
}
}
您需要覆盖 onResume(){},然后再次加载 url。
每当有人启动我的 app.Say 有人转到应用程序中的不同页面或转到后台然后重新启动应用程序时,我都试图刷新我的 webview 的内容,我想要我的 webview 的内容到 refresh/reload 基于我在我的服务器上所做的最新更改。 这是我目前的代码:
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()
是在activityreturns到前台的时候调用的。你可以把你的重载放在那里。像这样:
@Override
protected void onResume(){
super.onResume();
webView.reload();
}
您可以这样做,但请确保检查 WebView
不为空,因为 onResume
在 onCreate
之前被调用,而 WebView
可能不会还存在 onResume
!
@Override
protected void onResume() {
super.onResume();
if(webView != null){
webView.loadUrl(...);
or
webView.reload();
}
}
您需要覆盖 onResume(){},然后再次加载 url。