In my app i set the splash screen timer to 5 sec and later on think that 5 sec is too long so i change it back to 1 sec and my splash screen doesn't seen on the screen and keep me waiting for more than 5 sec i couldn't find what is wrong so here is my Splashscreen code
public class Splash extends Activity
{
private Timer_Countdown timer_Countdown = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
timer_Countdown = new Timer_Countdown(5000, 1000);
timer_Countdown.start();
}
class Timer_Countdown extends CountDownTimer
{
public Timer_Countdown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
timer_Countdown.cancel();
Intent startIntent;
startIntent = new Intent("android.intent.action.MAINMENU");
startActivity(startIntent);
}
#Override
public void onTick(long millisUntilFinished) {
}
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
And one last thing if I change it back to 5 sec it shows up on the screen again.
Why you are using this much of code just to use splash screen. Make it simple, you can use below code.
public class Splash extends Activity {
Timer timer = new Timer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
timer.schedule(new TimerTask() {
public void run() {
Intent intent = new Intent(Splash.this, NewActivity.class);
startActivity(intent);
finish();
}
}, 2000);
}
}
You can use Handler also
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
startActivity(new Intent(SplashActivity.this, YourNewActivity.class));
finish();
}
}, 3000);
or Using Timer with Timer Schedule
public class Splash extends Activity {
Timer t= new Timer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
t.schedule(new TimerTask() {
public void run() {
Intent n= new Intent(Splash.this, YourNewActivity.class);
startActivity(n);
}
}, 3000);
}
}
Use this instead of Timer
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//code for starting new activity
}
}, 5000);
Related
So I have this code, that has a countdown timer in a service with a 10 second timer. What I want to do is in the onFinish() method I want to launch the Activity (which is called MainActivity) automatically even when I am outside the app.
public class TimeDisplayTimerTask extends TimerTask{
CountDownTimer timer;
NotificationCompat.Builder notification;
private static final String TAG="com.timer";
private Handler mHandler = new Handler();
#Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
#Override
public void run() {
// display toast
timer = new CountDownTimer(10000, 1000) {
#Override
public void onFinish(){
}
#Override
public void onTick(long millisUntilFinished) {
Log.i(TAG,"" + millisUntilFinished/1000);
}
};
timer.start();
}
});
}
}
Try This to open your main activty after 10 second.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// launch your main activity here and finish your current activity
}
}, 10000);
}
Probably you will have to override lifecycle method's and also use PARTIAL_WAKE_LOCK to keep cpu running untill you finish execution, in case user lock screen.
If you are inside a service then simply launch you activity using intent. Put this code in your onFinish() method:
Intent i = new Intent();
i.setClass(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
You neet to pass an activity context to your TimeDisplayTimerTask :
public class TimeDisplayTimerTask extends TimerTask{
CountDownTimer timer;
NotificationCompat.Builder notification;
private static final String TAG="com.timer";
private Handler mHandler = new Handler();
private Activity mActivity;
public TimeDisplayTimerTask(Activity activity){
mActivity = activity;
super();
}
#Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
#Override
public void run() {
// display toast
timer = new CountDownTimer(10000, 1000) {
#Override
public void onFinish(){
if (activity != null {
Intent startIntent = new Intent(activity, MainActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(startIntent);
}
}
#Override
public void onTick(long millisUntilFinished) {
Log.i(TAG,"" + millisUntilFinished/1000);
}
};
timer.start();
}
});
}
}
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);
}
So I have a very stupid problem. I am able to create a simple timer task that ticks every second:
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (counter == 0) {
//Reload Map...
timer.cancel();
Intent intent = new Intent(MapsActivity.this, MapsActivity.class);
startActivity(intent);
} else {
TimeLabel.setText(Integer.toString(counter));
counter = counter - 1;
}
}
});
}
}, 0, 1000);
After one minute, it will reload my google map. But when I try to open other activity, and try to go back to Map, my MapsActivity is not killed, (even though I used finish();). That's is why my timer is overlapping.
I have a guess that I should create the timer OUTSIDE onCreate, but didn't success in doing so. Can you help me guys?
Try this
public class MainActivity extends Activity {
//Global Variables
Timer timer;
TimerTask task;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Make a method to make it cleaner
setTimer();
}
private void setTimer() {
timer = new Timer();
task = new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (counter == 0) {
//Reload Map...
timer.cancel();
Intent intent = new Intent(MapsActivity.this, MapsActivity.class);
startActivity(intent);
} else {
TimeLabel.setText(Integer.toString(counter));
counter = counter - 1;
}
}
});
}
};
}
#Override
protected void onResume() {
super.onResume();
//This will be called after onCreate or your activity is resumed
timer.schedule(task, 0, 1000);
}
#Override
protected void onPause() {
super.onPause();
//This will be called if the app is sent to background or the phone is locked
//Also this prevent you from duplicating the instance of your timer
timer.cancel();
}
}
i'm trying to use two timer in an activity,i have an imagview who i need to setivisible after 200 ml and after 4000ml new intent starts,my code is this:
public class welcome extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.welcome);
Timer timer=new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
ImageView img1=(ImageView) findViewById(R.id.welcome_img1);
img1.setVisibility(View.INVISIBLE);
}
},200);
Timer timer1=new Timer();
timer1.schedule(new TimerTask() {
#Override
public void run() {
finish();
Intent intent=new Intent(welcome.this, MainActivity.class);
startActivity(intent);
}
},4000);
super.onCreate(savedInstanceState);
}
}
thanks for your helps
Try this way: using Runnable and Handler
Runnable r2=new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
ImageView img1=(ImageView) findViewById(R.id.welcome_img1);
img1.setVisibility(View.INVISIBLE);
}
};
Handler h2=new Handler();
h2.postDelayed(r2,200);
and
Runnable r3=new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
finish();
Intent intent=new Intent(welcome.this, MainActivity.class);
startActivity(intent);
}
};
Handler h3=new Handler();
h3.postDelayed(r3,4000);
Use countdown timer
CountDownTimer timer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timer.start();
timer = new CountDownTimer(4000, 200) {
#Override
public void onTick(long millisUntilFinished) {
ImageView img1=(ImageView) findViewById(R.id.welcome_img1);
img1.setVisibility(View.INVISIBLE);
}
#Override
public void onFinish() {
finish();
Intent intent=new Intent(welcome.this, MainActivity.class);
startActivity(intent);
}
};
}
Hope this helps.
I use it to call another activity
Main.java
Intent intent = new Intent(this, Message_Note.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Message_Note.java :
public class Message_Note extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message);
}
}
How can i CLOSE the Message_Note Activity after 10 seconds ?? i should use a thread ?
After 100 MS, the activity will finish using the following code.
public class Message_Note extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
finish();
}
}, 100);
}
}
You can use following approach.
Approach 1
int finishTime = 10; //10 secs
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
YourActivity.this.finish();
}
}, finishTime * 1000);
Approach 2
int FinishTime = 10;
int countDownInterval = 1000;
counterTimer = new CountDownTimer(FinishTime * 1000, countDownInterval) {
public void onFinish() {
//finish your activity here
}
public void onTick(long millisUntilFinished) {
//called every 1 sec coz countDownInterval = 1000 (1 sec)
}
};
counterTimer.start();
You can use AlarmManager. See :
http://developer.android.com/reference/android/app/AlarmManager.html
and
Alarm Manager Example
Another way is just like this:
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Message_Note.this.finish();
}
}, 10000);