Reputation: 4187
I want to show the splash screen while the webview loads. How ever in this code it waits for the time out time(30 sec) and then shows the webview. I don't know what I am missing ?
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);
}
}
And here is the Splash Activity
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();
}
}
Upvotes: 0
Views: 559
Reputation: 625
What @abc667 means is that you must extend the WebViewClient in this way so it will be called:
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished (WebView view, String url) {
synchronized (SPLASH_LOCK) {
SPLASH_LOCK.notifyAll();
}
}
}
Upvotes: 0
Reputation: 153
You can Set background of your main activity instead, it will work the same
Upvotes: 1
Reputation: 514
Method onPageFinished
from MainActivity
will be never called. Try to extend WebViewClient
class and override onPageFinished
. Then create object of this class here
mywebview.setWebViewClient(new CustomWebViewClient());
Upvotes: 1