I'd appreciate your help in interrupting an Android/Java sleep. What I have in my layout is a button, which if clicked, calls the Skip method and starts a new activity. FYI The same activity would be called anyway when the Sleep method terminates.
Here's my failing code:
public class Splash extends Activity {
private Thread timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch (Exception e) {
e.printStackTrace();
onPause();
return;
} finally {
onPause();
startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
}
}
};
timer.start();
}
#Override
public void onPause() {
timer.interrupt();
super.onPause();
finish();
}
public void Skip() {
timer.interrupt();
startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
}
Now Resolved!
I've now got it all working. In addition to #RocketSpock's suggestions there was also a stupid error in my code in that I'd failed to include the View view paramater into my Skip method call. So the fully working code now looks like this:
public class Splash extends Activity {
private Thread timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
timer = new Thread() {
public void run() {
synchronized (this) {
try {
sleep(5000);
} catch (Exception e) {
e.printStackTrace();
onPause();
return;
} finally {
onPause();
startActivity(new Intent(
"net.rogw.splashscreenexample.MainActivity"));
}
}
}
};
timer.start();
}
#Override
public void onPause() {
timer.interrupt();
super.onPause();
finish();
}
public void Skip(View view) {
synchronized (this) {
this.notify();
}
startActivity(new Intent("net.rogw.splashscreenexample.MainActivity"));
}
}
If you want to be able to interrupt it you should be using a wait.
public class Splash extends Activity {
private Thread timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
timer = new Thread() {
public void run() {
synchronized(this) {
try {
wait(5000);
} catch (Exception e) {
e.printStackTrace();
onPause();
return;
} finally {
onPause();
startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
}
}
}
};
timer.start();
}
#Override
public void onPause() {
timer.interrupt();
super.onPause();
finish();
}
public void Skip() {
//You may need to replace this with the timer object
synchronized (this) {
//Informs the wait to interrupt.
this.notify();
}
startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
}
Related
I created two Activities "SplashActivity and AuthenticationActivity" When I start the app it loads the splash screen for 5 seconds and then it moves on to AuthenticationActivity. My problem is that when I run the app, it loads the splash screen, but within 5 seconds when I click the back button, SplashActivity exits, and immediately AuthenticationActivity appears in the foreground.
Does anyone know how to make it so when I click the back button, my app exits?
Here's my code so far:
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread splashTimer = new Thread(){
public void run(){
try{
sleep(5000);
startActivity(new Intent(SplashActivity.this,AuthenticationActivity.class));
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
finish();
}
}
};
splashTimer.start();
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
}}
Timer timer;
TimerTask timerTask;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
initialize();
}
private void initialize()
{
timer = new Timer();
timerTask = new TimerTask()
{
#Override
public void run()
{
//Start your activity here
}
};
timer.schedule(timerTask,2500);
}
#Override
public void onBackPressed() {
super.onBackPressed();
timer.cancel();
}
or you can use Handler also
Handler handler;
Runnable runnable;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
initialize();
}
private void initialize()
{
handler = new Handler();
runnable = new Runnable()
{
#Override
public void run()
{
//start your activity here
}
};
handler.postDelayed(runnable, 5000);
}
#Override
public void onBackPressed() {
super.onBackPressed();
handler.removeCallbacks(runnable);
}
The following code is from Beginning Android 3, Chapter 20. When the phone is rotated, a new activity will be created and onStart() will be called, and so bar.setProgress(0) is called. However, I don't see the bar's progress is back to the beginning. Why not?
public class HandlerDemo extends Activity {
ProgressBar bar;
Handler handler=new Handler() {
#Override
public void handleMessage(Message msg) {
bar.incrementProgressBy(5);
}
};
AtomicBoolean isRunning=new AtomicBoolean(false);
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
bar=(ProgressBar)findViewById(R.id.progress);
}
public void onStart() {
super.onStart();
bar.setProgress(0);
Thread background=new Thread(new Runnable() {
public void run() {
try {
for (int i=0;i<20 && isRunning.get();i++) {
Thread.sleep(1000);
handler.sendMessage(handler.obtainMessage());
}
} catch (Throwable t) {
// just end the background thread
}
}
});
isRunning.set(true);
background.start();
}
public void onStop() {
super.onStop();
isRunning.set(false);
}
}
Try using this code
#Override
protected void onPause() {
super.onPause();
isRunning.set(false);
bar.setProgress(0);
}
I want to change the view/Activity of my app after few seconds
I mean i have created a home View for my app and i want to move to the next Activity after like 3 seconds, How should I achieve that.
Thank You
try this,
Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
Intent i = new Intent(FirstActivity.this,SeconActivity.class);
startActivity(i);
};
};
mHandler.sendEmptyMessageDelayed(0, 3000);
You can make slash Activity. Try this code....hop your problem will solve
public class SplashActivity extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(SplashActivity.this, NightClubMain.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGHT);
}
view.postDelayed(Runnable r, int delay);
You can use Timer for that.
Timer myTimer;
startTimerTask();
public void startTimerTask() {
MyTimerTask myTask = new MyTimerTask();
myTimer = new Timer();
myTimer.schedule(myTask, 0, 3000);
}
#Override
public void onPause() {
super.onPause();
try {
myTimer.cancel();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onStop() {
super.onStop();
try {
myTimer.cancel();
} catch (Exception e) {
e.printStackTrace();
}
}
class MyTimerTask extends TimerTask {
public void run() {
try {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
//
Do YOUR STUFF HERE
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
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
At first my splash screen works perfectly, however later I tried to put in a code which would destroy the activity splash. I did this by putting the onPause method into the end of the protected void.
This is the splash screen before putting in the method
'package com.shipment.emulatorfix;
'import android.app.Activity;
'import android.content.Intent;
'import android.media.MediaPlayer;
'import android.os.Bundle;
'public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY");
startActivity(openMain);
}
}
};
timer.start();
}
'}
this is the after code
'package com.shipment.emulatorfix;
'import android.app.Activity;
'import android.content.Intent;
'import android.media.MediaPlayer;
'import android.os.Bundle;
'public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY");
startActivity(openMain);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
'}
Any help would be greatly appreciated, thank you.
try this
public class SplashScreen extends Activity {
protected int _splashTime = 2000;
private Thread splashTread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
splashTread = new Thread() {
#Override
public void run() {
try {
synchronized(this) {
wait(_splashTime);
}
} catch(InterruptedException e) {
System.out.println("EXc=" + e);
}
finally {
startActivity(new Intent(SplashScreen.this, Login.class ));
//stop();
finish();
}
}
};
splashTread.start();
}
}
public class Welcome extends Activity
{
/** Called when the activity is first created. */
Handler mHandler,actHandler;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
new Thread(){
public void run(){
try{
Thread.sleep(3000);
}
catch(Exception ex){
Log.e("Welcome Exception :",ex.toString());
}
try{
Message msg=mHandler.obtainMessage();
mHandler.sendMessage(msg);
}
catch(NullPointerException ex){
Log.e("Handler Exception :",ex.toString());
}
}
}.start();
mHandler=new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
Intent i=new Intent(Welcome.this,M_chat.class);
startActivity(i);
finish();
}
};
}
}
Finish the activity and then start the other activity.
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch(InterruptedException e){
e.printStackTrace();
}finally{
finish();
Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY");
startActivity(openMain);
}
}
};
This should work for you.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch(InterruptedException e){
e.printStackTrace();
} finally {
Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY");
startActivity(openMain);
Splash.this.finish();
}
}
};
timer.start();
}