【发布时间】:2014-09-08 10:18:26
【问题描述】:
我的应用中有一个 WebView,我希望它永远运行。
当我返回我的应用时(onResume),WebView 是黑屏...
我尝试了多种解决方案和布局。我正在使用函数 setContentView(w);
添加 WebView在第一次运行时它运行良好。当我按下主页按钮时,WebView 仍在运行(console.log(),来自 javascript 提供反馈)。回到应用程序,我看到一个黑屏。 WebView 仍在运行,但我看不到它:(。
我也尝试将其添加到 .xml 视图文件中,但没有结果。我还想防止页面重新加载。我试图将视图设置为可见。尝试了一些功能,如 w.invalidate()。
MainActivity.java
public class MainActivity extends Activity {
public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("main"," create ");
if (savedInstanceState == null) {
Intent i = new Intent(MainActivity.this, TestService.class);
TestService.setMain(this);
TestService.setView();
MainActivity.this.startService(i);
} else {
TestService.setAgain();
}
}
@Override
public void onBackPressed() {
Log.v("main","back press");
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
}
TestService.java
public class TestService extends Service {
private static WebView w;
private static MainActivity ma;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
public static void setMain(MainActivity a) {
ma = a;
Log.e("main", " setData " );
}
public static void setView() {
Log.e("TestService", "FIRST TIME SETUP");
w = new WebView(ma);
ma.setContentView(w);
w.getSettings().setDomStorageEnabled(true); // use localstorage
w.getSettings().setJavaScriptEnabled(true);
w.loadUrl("file:///android_asset/www/index.html");
}
public static void setAgain() {
w.bringToFront();
w.loadUrl("javascript: test();");
Log.e("TestService", w.getVisibility() + ""); // returns 0 (View.VISIBLE)
}
}
index.html
<html>
<head>
<title>Webview test Android</title>
<style>
body {
background: white;
}
html {
min-width:100%;
min-height:100%;
}
</style>
</head>
<body>
Counter<br>
<h1><div id="counter"></div></h1>
<script>
var counter = 0;
document.getElementById("counter").innerHTML = counter;
setTimeout(function(){ count(); }, 1000);
function test() {
console.log("Test");
alert("Jooo");
}
function count() {
counter++;
document.getElementById("counter").innerHTML = counter;
setTimeout(function(){ count(); }, 1000);
console.log("count " + counter);
}
</script>
<font color="#ff0">Test</font>
</body>
</html>
也许有人知道正确的方法吗?
【问题讨论】:
标签: android view webview onresume onpause