Splash screen doesn't work - android

public class tryAnimActivity extends Activity
{
/**
* The thread to process splash screen events
*/
private Thread mSplashThread;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Splash screen view
setContentView(R.layout.janman);
final tryAnimActivity sPlashScreen = this;
// The thread to wait for splash screen events
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
wait(5000);
}
}
catch(InterruptedException ex){
}
finish();
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen, TabsActivity.class);
startActivity(intent);
stop();
}
};
mSplashThread.start();
}
/**
* Processes splash screen touch events
*/
#Override
public boolean onTouchEvent(MotionEvent evt)
{
if(evt.getAction() == MotionEvent.ACTION_DOWN)
{
synchronized(mSplashThread){
mSplashThread.notifyAll();
}
}
return true;
}
}
whats the problem with this code? On clicking, it crashes down. Also, the next activity does not start after the end of this activity. This is the activity used for displaying splashscreen.

I prefer to use Runnable element instead of Thread. My splash code for the activity below works well. I hope it helps you:
public class SplashScreenActivity extends Activity implements OnTouchListener {
private int SPLASH_DISPLAY_LENGTH = 3000;
private Handler splashHandler;
private Runnable splashRunnable;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
findViewById(R.id.splash_img).setOnTouchListener(this);
splashHandler = new Handler();
splashRunnable = new Runnable()
{
#Override
public void run()
{
SplashScreenActivity.this.finish();
Intent mainIntent = new Intent(SplashScreenActivity.this, HomeActivity.class);
startActivity(mainIntent);
}
};
splashHandler.postDelayed(splashRunnable, SPLASH_DISPLAY_LENGTH);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
splashHandler.removeCallbacks(splashRunnable);
SplashScreenActivity.this.finish();
Intent mainIntent = new Intent(SplashScreenActivity.this, HomeActivity.class);
startActivity(mainIntent);
}
return true;
}
}

just remove stop() method it will work

Related

How to make the application close if splash activity is closed in Android

I have a splash activity that is displayed for 2 seconds before opening the main activity.
If the user presses the back button while the splash activity is being displayed, the splash activity closes. But a short time later, the main activity [which was triggered by the splash activity] opens up.
I don't want this to happen. I want the entire application to close if the back button is pressed while the splash screen is being displayed.
How do I accomplish this?
Edit:
below is my code for splash activity:
public class Splash2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// fading transition between activities
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.activity_splash2);
Thread timer = new Thread() {
public void run() {
try {
sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent open = new Intent(
"com.example.puzzletimer.HOMESCREEN");
startActivity(open);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
Just add this code to your SplashActivity...
#Override
public void onBackPressed() {
super.onBackPressed();
android.os.Process.killProcess(android.os.Process.myPid());
}
or maintain one flag to determine to start Activity or not in Thread...
public class Splash2 extends Activity {
private volatile boolean interrupt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// fading transition between activities
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.activity_splash2);
Thread timer = new Thread() {
public void run() {
try {
sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (!interrupt) {
Intent open = new Intent(
"com.example.puzzletimer.HOMESCREEN");
startActivity(open);
}
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
#Override
public void onBackPressed() {
super.onBackPressed();
interrupt = true;
}
}
Try this-
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
Another way to do it is with runnable:
Declare Handler and Runnable in your Splash2 activity:
private Handler handler;
private Runnable startMain;
In onCreate(), initialize them and set the runnable to fire in 1.5 secs:
handler = new Handler();
startMain = new Runnable() {
#Override
public void run() {
Intent open = new Intent("com.example.puzzletimer.HOMESCREEN");
startActivity(open);
}
};
handler.postDelayed(startMain, 1500);
Override onBackPressed() and simply cancel runnable:
#Override
public void onBackPressed() {
handler.removeCallbacks(startMain);
super.onBackPressed();
}
That's it. If back key is pressed, it cancels runnable and your homescreen won't run.

how can i load 2 activities, show the first and after faw seconds the second? [duplicate]

This question already has answers here:
Splash screen while loading a url in a webview in android app
(3 answers)
Closed 9 years ago.
I want to load a website (in WebView), but it loads too slow and show a white screen. I want to show a splash screen and in the background load the WebView. After few seconds I want to close the splash screen and show the site that should be ready by then. How can I do it?
Thanks!
I use this code for a splash screen
public class SplashActivity extends Activity {
private boolean isBackButtonPressed;
private static final int SPLASH_DURATION = 2000; // 2 seconds
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.SplashTheme);
getWindow().setWindowAnimations(0);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
Handler handler = new Handler();
// run a thread after 2 seconds to start the home screen
handler.postDelayed(new Runnable() {
#Override
public void run() {
// make sure we close the splash screen so the user won't come
// back when it presses back key
finish();
if (!isBackButtonPressed) {
// start the home screen if the back button wasn't pressed
Intent intent = new Intent(SplashActivity.this, FragmentActivity.class);
startActivity(intent);
finish();
}
}
}, SPLASH_DURATION);
}
#Override
public void onBackPressed() {
super.onBackPressed();
isBackButtonPressed = true;
}
}
You can use the following code for it.
public class Splash extends Activity {
/** Called when the activity is first created. */
private boolean mSplashActive = true, mPaused;
private long mSplashTime = 1000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Thread(){
public void run(){
try {
long ms = 0;
while(mSplashActive && ms < mSplashTime) {
sleep(100);
if(!mPaused) {
ms += 100;
}
}
if(Resources.getResources().isUserRegistered(Splash.this)) {
//user is registered so launch main screen
} else {
//user is not registered launch welcome wizard.
Intent intent = new Intent(Splash.this, WelcomeScreen.class);
Splash.this.startActivity(intent);
}
finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
#Override
public void onPause(){
super.onPause();
mPaused = true;
}
#Override
public void onResume(){
super.onResume();
mPaused = false;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
mSplashActive = false;
}
return true;
}
}
in the webview class you can set something like this.
i setup this for you:
public class WebViewActivity extends Activity {
WebView webView;
ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
progressDialog = new ProgressDialog(WebViewActivity.this);
progressDialog.setTitle("here your title");
progressDialog.setMessage("message here");
webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
// here you can display an imageview fullscreen
// or show and progressdialog
progressDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
// here you can remove the imageview and the user can continue using the webview
// or hide the progressdialog
progressDialog.dismiss();
}
});
}
}
if you have any questions let me know
public class SplashScreen extends Activity {
private Thread mSplashThread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
final SplashScreen sPlashScreen = this;
// The thread to wait for splash screen events
mSplashThread = new Thread() {
#SuppressWarnings("deprecation")
#Override
public void run() {
try {
synchronized (this) {
// Wait given period of time or exit on touch
wait(5000);
}
} catch (InterruptedException ex) {
}
finish();
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen, MainActivity.class);
startActivity(intent);
}
};
mSplashThread.start();
}
/**
* Processes splash screen touch events
*/
#Override
public boolean onTouchEvent(MotionEvent evt) {
if (evt.getAction() == MotionEvent.ACTION_DOWN) {
synchronized (mSplashThread) {
mSplashThread.notifyAll();
}
}
return true;
}
}

overridePendingTransition not working on android 2.3.5

This code of mine is not working... I have checked all the links on this site and also tried animation listener but still its not working.
public class SplashScreenPage extends Activity implements Runnable{
Thread splash;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen_page_layout);
splash = new Thread(this);
splash.start();
}
#SuppressWarnings("static-access")
#Override
public void run() {
try {
splash.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent intent = new Intent(SplashScreenPage.this,LoginPage.class);
startActivity(intent);
finish();
SplashScreenPage.this.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
}
#Override
protected void onPause() {
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
super.onPause();
}
}
The problem is with your device's default settings. Go to settings > display > animation > allow "all animations". This will allow overridePendingTransition to work properly.
The problem I see is that you're using your animation in a different Thread of the main thread. That main thread is also known as the UI Thread. So, you need to get back to that UI Thread. This should work (I use overridePendingTransition(0, 0) to remove any animation, you can experiment with others:
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_splash);
presentLogo();
}
/** Called to present the Splash image for an amount of time */
private void presentLogo() {
final SplashActivity splashActivity = this;
new Thread() {
public void run() {
synchronized (splashActivity) {
try {
sleep(Constants.SPLASH_PRESENTATION_DURATION);
} catch (InterruptedException e) {
} finally {
runOnUiThread(new Runnable() {
public void run() {
finish();
overridePendingTransition(0, 0);
// After splash, go to the new activity
Intent intent = new Intent();
intent.setClass(splashActivity, LoginActivity.class);
startActivity(intent);
}
});
}
}
}
}.start();
}
}
Try this:
Intent intent = new Intent(SplashScreenPage.this,LoginPage.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);

Splash Image for android

I made a splash image to show at the start of my activity..
The image show perfectly.But the problem is when i call this
public class SplashImageActivity extends Activity {
protected boolean active = true;
protected int splashTime = 5000; // time to display the splash screen in ms
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
// thread for displaying the SplashScreen
Thread splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while(active && (waited < splashTime)) {
sleep(100);
if(active) {
waited += 100;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
startActivity(new Intent(SplashImageActivity.this,Myapps.class));
finish();
//startActivity(new Intent("com.splash.com.MyApps"));
//startActivity( new Intent(getApplicationContext(), Myapps.class));
}
}
};
splashTread.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
active = false;
}
return true;
}
}
go for next activity the stop() does not work. And it does not go to this activity. I add all activity in manifest. The stop() shows in code like this
what's the problem?
No need to call stop() and call finish() after starting activity
finally
{
startActivity(new Intent(currentclass.this,nextActivity.class);
finish();
}
I use thread to show the Splash screen, and it works for me:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
wait(4000);
}
}catch(InterruptedException ex){
}
finish();
Intent i=new Intent(getApplicationContext(),NextActivity.class);
startActivity(i);
interrupt();
}
};
mSplashThread.start();
}
Please try below code..
public class Splashscreen extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Thread t2 = new Thread() {
public void run() {
try {
sleep(2000);
startActivity( new Intent(getApplicationContext(), Exercise.class));
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
t2.start();
}
}
No need to call stop() just call finish() after starting activity
finally {
startActivity(new Intent(currentclass.this,nextActivity.class);
finish();
}
You can also use handler an postdelayed() to make a splash screen like below
public class SplashScreenActivity extends Activity{
private Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
final Runnable runnable = new Runnable() {
#Override
public void run() {
Intent intent=new Intent(SplashScreenActivity.this, nextActivity.class);
startActivity(intent);
finish();
}
};
handler = new Handler();
handler.postDelayed(runnable, 5000);
}
}
You will show your splash screen for 5 seconds and then move to next Activity
first thing it is not onStop of Activity so looks you are calling stop function of thread which is Deprecated that's why you are getting the strike line so use other way to stop the thread of use better way to implement the splash ........
as looks you try some thing like this link

Android equivalent of iPhone's default image

Is it possible to set a default image during starting an application? On iPhone, I can set an image named "Default.png" to do that. But is it possible on Android?
(For information I ask that because I can't use a splascreen view or auto-activity to an other, because of videoview in the first activity.)
I dont understand a reason why not to use splashscrean
public class SplashScreenActivity extends Activity {
protected boolean _active = true;
private Thread splashTread;
protected int _splashTime = 2000; // time to display the splash screen in ms
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
// thread for displaying the SplashScreen
final SplashScreenActivity sPlashScreen = this;
// thread for displaying the SplashScreen
splashTread = new Thread() {
#Override
public void run() {
try {
synchronized (this) {
// wait 5 sec
wait(_splashTime);
}
} catch (InterruptedException e) {
} catch (UnsupportedOperationException e1) {
// TODO: handle exception
} finally {
finish();
// start a new activity
Intent i = new Intent();
i.setClass(sPlashScreen, Your videoViewActivity.class);
startActivity(i);
}
}
};
splashTread.start();
}
// Function that will handle the touch
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
synchronized (splashTread) {
splashTread.notifyAll();
}
}
return true;
}
}

Categories

Resources