gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:19:06 26 4
gpt4 key购买 nike

我看到了很多与我的问题相关的文档和文章。但是我没有找到任何合适的解决方案。当我按下电源按钮打开/关闭时,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"。你们能为我的问题提供任何解决方案吗?

最佳答案

尝试

@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 重新加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46638986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com