using multiple timer in an activity - android

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.

Related

After Home Button get pressed i got activities switch ANDROID

I have my first activity that switch to the second one after 3 seconds and this works fine. The problem is that if i press the Home Button during this 3 seconds, the app reopen in the second activity. Is there a simple way to fix this?
Thanks in advance.
public class StartActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
switchActivities();
}
#Override
public void onBackPressed(){
}
public void switchActivities() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
finish();
}
}, 3000);
}
}
EDIT:
Maybe I wasn't clear, I do not want the app to reopen once I press the home button. How can I do this?
This is because you don't clear your delayed callback. You can fix it in this way:
private Handler handler = new Handler();
public void switchActivities() {
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
finish();
}
}, 3000);
}
public void onStop() {
super.onStop();
handler.removeCallbacksAndMessages(null);
}
You just have to cancel your Handler when you leave your first activity before the second one opens.
public class StartActivity extends AppCompatActivity {
Runnable nextActivityRunnable;
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
switchActivities();
}
#Override
public void onBackPressed(){
}
public void switchActivities() {
nextActivityRunnable = new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(),
SecondActivity.class);
startActivity(intent);
finish();
}
};
handler = new Handler();
handler.postDelayed(nextActivityRunnable, 3000);
}
#Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(nextActivityRunnable);
}
}

How to create Timer Task OUTSIDE onCreate in Android Studio?

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();
}
}

Android Splash Screen Timer Does not Work Properly

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);

Switching a View/Activity after Few seconds

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();
}
}
}

Close an Activity after 10 seconds?

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);

Categories

Resources