Android如何在屏幕关闭/打开时防止webview重新加载?

Posted

技术标签:

【中文标题】Android如何在屏幕关闭/打开时防止webview重新加载?【英文标题】:Android how to prevent webview reload when the screen is off/on? 【发布时间】:2018-03-20 05:20:36 【问题描述】:

我看到了很多与我的问题相关的文档和文章。但我没有得到任何适当的解决方案。当我按下电源按钮打开/关闭时,WebView 将被重新加载。我不想重新加载WebView

我正在尝试的是......

protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    StrictMode.ThreadPolicy policy = new 
    StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    loadpages();

private BroadcastReceiver receiver = new BroadcastReceiver() 
    @Override
    public void onReceive(Context context, Intent intent) 
        if (!ConnectivityStatus.isConnected(context)) 
            // no connection
            WebView wv = (WebView) findViewById(R.id.webView1);
            wv.loadData(iError, "text/html", "UTF-8");
            // Toast.makeText(getApplicationContext(),"Internet connection failure",Toast.LENGTH_SHORT).show();
        
    
;

@Override
protected void onResume() 
    super.onResume();
    this.registerReceiver(receiver, new 
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

 @Override
protected void onPause() 
    super.onPause();
    this.unregisterReceiver(receiver);
 
@Override
public void onBackPressed() 
    AlertDialog.Builder alertDialog = new 
   AlertDialog.Builder(MainActivity.this);
    alertDialog.setTitle("Local");
    alertDialog.setMessage("Exit Application");
    alertDialog.setCancelable(true);
    alertDialog.setPositiveButton("YES", new 
    DialogInterface.OnClickListener() 
        public void onClick(DialogInterface dialog, int which) 
       // User pressed YES button. Write Logic Here
            MainActivity.this.finish();  
        
    );
    alertDialog.setNegativeButton("NO", new 
   DialogInterface.OnClickListener() 
        public void onClick(DialogInterface dialog, int which) 
            // User pressed No button. Write Logic Here
            //   Toast.makeText(getApplicationContext(), "You clicked on 
           NO", Toast.LENGTH_SHORT).show();
        
    );
    alertDialog.show();

@Override
public void onConfigurationChanged(Configuration newConfig) 
    super.onConfigurationChanged(newConfig);

@SuppressLint("NewApi")
public boolean loadpages() 
    WebView wv = (WebView) findViewById(R.id.webView1);
    WebSettings webSettings = wv.getSettings();
    webSettings.setjavascriptEnabled(true);
    wv.getSettings().setLoadWithOverviewMode(true);
    wv.getSettings().setUseWideViewPort(true);
    CookieManager.getInstance().setAcceptCookie(true);
     TelephonyManager mngr = (TelephonyManager) 
    getSystemService(Context.TELEPHONY_SERVICE);
    String imeino = mngr.getDeviceId().toString();
   CookieManager.getInstance().setCookie
   ("http://MyLocalHost/projects/aaaaa", "IMEINO=" + imeino);
    String url = "http://MyLocalHost/projects/aaaaa/index.php"
    wv.setWebViewClient(new WebViewClient() 
  public boolean shouldOverrideUrlLoading(WebView view, String url) 
    if (!loadingFinished) 
    redirect = true;
    
    loadingFinished = false;
    view.loadUrl(url);
    return true;
    
  @SuppressWarnings("unused")
  public void onPageStarted(WebView view, String url) 
    loadingFinished = false;
    // SHOW LOADING IF IT ISNT ALREADY VISIBLE
    
  public void onPageFinished(WebView view, String url) 
    if (!redirect) 
    loadingFinished = true;
    
    if (loadingFinished && !redirect) 
    // HIDE LOADING IT HAS FINISHED
     else 
    redirect = false;
     
    
    );
    loadingFinished = false;
    wv.loadUrl(url);
   return loadingFinished;
 

我正在尽力而为。我还在 AndroidManifest.xml 文件中添加了android:launchMode="singleTask"。你们能解决我的问题吗?

【问题讨论】:

检查你的广播接收器 onReceive()。如果你收到 onResume(),你的 webview 将在片段/活动恢复时加载。 @james 是的,我检查了。但没有得到任何解决方案 【参考方案1】:

尝试:

if (savedInstanceState == null)

web.loadUrl(webURL);

【讨论】:

在恢复和暂停时只检查网络连接 然后告诉我你的手机内存已满或者你有一些空间如果是然后告诉我剩余空间 如果您在 Activity 进入停止状态时将代码置于 onCreate() 方法中,并且如果设备内存较少,则进程将被终止并将内存分配给高优先级应用程序。返回时它从 oncreate() 方法恢复,这里不仅重新加载您的 webView,而且重新加载整个应用程序。【参考方案2】:

尝试在你的生命周期方法中添加:

@Override
protected void onSaveInstanceState(Bundle outState )

super.onSaveInstanceState(outState);
web.saveState(outState);


@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)

super.onRestoreInstanceState(savedInstanceState);
web.restoreState(savedInstanceState);

【讨论】:

在筛选后检查您的 webview 从哪里重新加载。我的意思是把登录 oncreate,onpause,onresume,loadpages 方法 @A2N 屏幕锁定应用调用 onpause 或 onStop.screen 解锁应用调用恢复。 设置webviewclient和js启用为true 1.在您开始加载 webview 的同时启动一个计时器。 2.在计时器内检查 isloadingFinished 并在需要时显示错误屏幕developer.android.com/reference/android/os/CountDownTimer.html【参考方案3】:

试试

@Override
protected void onStart() 
    super.onStart();
    this.registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));


@Override
protected void onStop() 
    super.onStop();
    this.unregisterReceiver(receiver);

而不是

@Override
protected void onResume() 
    super.onResume();
    this.registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
 

@Override
protected void onPause() 
    super.onPause();
    this.unregisterReceiver(receiver);

【讨论】:

以上是关于Android如何在屏幕关闭/打开时防止webview重新加载?的主要内容,如果未能解决你的问题,请参考以下文章

底部对话框打开时如何防止键盘关闭?

c#当屏幕/显示器电源关闭或打开时如何获取事件?

如何防止 Application.SetSuspendState 关闭系统?

如何在我的应用打开且屏幕打开时允许 Android 打瞌睡?

屏幕打开/关闭时如何更新 Android 小部件?

如何处理后退按钮android以退出应用程序并在首页中打开时关闭本机导航侧菜单