Webビューのロード中にスプラッシュ画面を表示したい。ただし、このコードでは、タイムアウトタイムアウト(30秒)を待ってからWebビューを表示します。何が足りないのかわかりませんか?
public class MainActivity extends Activity {
public static Object SPLASH_LOCK = new Object();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.setVerticalScrollBarEnabled(false);
mywebview.setHorizontalScrollBarEnabled(false);
mywebview.getSettings().setLoadWithOverviewMode(true);
mywebview.getSettings().setUseWideViewPort(true);
mywebview.getSettings().setRenderPriority(RenderPriority.HIGH);
mywebview.setWebViewClient(new WebViewClient());
mywebview.loadUrl("http://www.xxx.com/mobile/index.php?ver=1");
startActivity(new Intent(this, SplashActivity.class));
}
public void onPageFinished (WebView view, String url) {
synchronized (SPLASH_LOCK) {
SPLASH_LOCK.notifyAll();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView mywebview = (WebView) findViewById(R.id.webview);
if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
mywebview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
そしてここにスプラッシュアクティビティがあります
public class SplashActivity extends Activity {
private static String TAG = SplashActivity.class.getName();
private static long MAX_SPLASH_TIME = 30000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar
setContentView(R.layout.splash);
new Thread() {
@Override
public void run() {
synchronized (MainActivity.SPLASH_LOCK) {
// wait for notify or time-out
try { MainActivity.SPLASH_LOCK.wait(MAX_SPLASH_TIME); }
catch (InterruptedException ignored) {}
}
finish();
}
}.start();
}
}