Im trying to make a timer for a game, and I'm stuck atm.
This is my code so far. I made a new activity for the intent, cause I read somewhere that's one way to do it.
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Timer timer = new Timer();
//task = new TimerTask() {
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent("com.loltimer.Timer"));
}
});
};
}
When I press the first button I want to have a timer that goes from 5minutes down to 0. And at 0 I want a sound to be played.
Thanks in advance!
Has got to be the Android count down timer class: http://developer.android.com/reference/android/os/CountDownTimer.html
Timing operations are best handled by a Handler in Android.
http://developer.android.com/reference/android/os/Handler.html
You can wrap the operation you want to run in the future as a Runnable and pass it to postDelayed(). If the operation needs to run repeatedly on that interval, you can call postDelayed() at the end of the Runnable to schedule the next run.
CountdownTimer can also be useful if you need to run some code a finite number of times on a regular interval. This class is actually just a thin wrapper around Handler.
Related
I am just starting to learn Android, Java and need help.
I have an activity with the countdowntimer, which works fine. However, I want it to be displayed in the fragment. What is the best way to do it?
I tried calling Timer.getCountdowntimer, I tried calling Timer.getUserTime (userTime is the user selected time for the countdowntimer), but the textview in my fragment doesn't display the timer.
thanks in advance!
If you are coding in Java purely, and want to use the android SDK to do it, I would recommend:-
//Create a handler that runs on main loop so we can update UX
final android.os.Handler handler = new android.os.Handler(Looper.getMainLooper());
//Get a callback in 1 second
handler.postDelayed(new Runnable() {
int timer;
#Override
public void run() {
timer += 1;
myTextView.setText(String.valueOf(timer));
//Recursively get another callback in a second
handler.postDelayed(this, 1000);
}
}, 1000);
Make sure you add some logic to stop the timer when you want, and also onPause/onResume
I am new in Android Development. I have been trying to figure out how to display a screen in android studio only for 5 seconds and then get transfered into a new activity.
For example:
Activity A -> Activity B (Shown for 5 seconds) -> Activity C
Also I want to make sure that when a user clicks on the back button while he is in Activity B nothing happens (It doesnt go back to Activity A).
What is the easiest way to do that?
I know I have to use Intent.
try this. I have commented it out, but if you have any questions about it feel free to ask.
public class ClassB extends AppCompatActivity {
//Handler allows you to send and process Runnable Objects (Classes in this case)
private Handler mHandler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_classb);
//postDelayed method, Causes the Runnable r (in this case Class B) to be added to the message queue, to be run
// after the specified amount of time elapses.
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
//Create a new Intent to go from Class B to Class C and start the new Activity.
Intent intent = new Intent(ClassB.this, ClassC.class);
startActivity(intent);
finish()
}
//Here after the comma you specify the amount of time you want the screen to be delayed. 5000 is for 5 seconds.
}, 5000);
}
//Override onBackPressed method and give it no functionality. This way when the user clicks the back button he will not go back.
public void onBackPressed() {
} }
In Kotlin you can do:
Handler().postDelayed({
// Start activity
startActivity(Intent(this, YourTargetActivity::class.java))
// terminate this activity(optional)
finish()
}, 5000)
I'am a beginner.
I have a timer in my puzzle game. But there is a bug. When the user turns off the screen, the timer doesn`t stop. If user turns screen on again he can see game over screen and this is not good from UX perspective.
Here is my code for timer
private void initializeProgressBar() {
//initialize progressbar
progress = ApplicationConstants.GAME_TIME;
mProgress = (ProgressBarDetermininate) findViewById(R.id.progressDeterminate);
mProgress.setMax(progress);
mProgress.setProgress(progress );
timer = new Timer();
progressBarUpdateTask = new ProgressBarUpdateTask();
timer.schedule(progressBarUpdateTask, 20, 20);
}
class ProgressBarUpdateTask extends TimerTask {
#Override
public void run() {
runOnUiThread(new Runnable(){
#Override
public void run() {
progress-=1;
if(progress==0)
{
TimeOver();
}
mProgress.setProgress(progress);
}
});
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onPause() {
super.onPause();
this.timer.cancel();
}
I can`t figure out how to fix this onResume, because now timer stops when user turn off the screen, but doesnt resume when user turn on screen.
Then you should pause the timer in your onPause() method not cancel it and in the onResume() method you could restart the timer with the seconds which were left, before the screen has been turned off. For further informations about the lifecycle of an activity have a look at the Activity Lifecycle.
But what I think you really need is the CountDownTimer class not the Timer class.
look into CountDownTimer. The timer you are using now is not supposed to really be used like that. Unfortunately there is no pause so onTick save a variable to know what the timer is currently at, then on pause cancel the timer. then in onresume create a new time and set the time to the variable you are using to keep track of the last timer.
http://developer.android.com/reference/android/os/CountDownTimer.html
Hi everyone out there,
i am developing an android application against API 7 at the moment in which i use an activity which need to be restarted. Lets say my activity looks like this:
public class AllocActivity extends Activity implements OnClickListener{
Button but;
private Handler hand = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_alloc);
but = (Button) findViewById(R.id.button);
but.setText("RELOAD");
but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0){
Intent intent = getIntent();
startActivity(intent);
finish();
}
});
}
#Override
protected void onDestroy(){
super.onDestroy();
System.gc();
}
/****** THREADS AND RUNNABLES ******/
final Runnable fullAnim = new Thread(new Runnable(){
#Override
public void run(){
try{
hand.post(anim1);
Thread.sleep(2000);
hand.post(anim2);
Thread.sleep(1000);
// and so on
}catch(InterruptedException ie){ie.printStackTrace();}
}
});
final Runnable anim1 = new Runnable() {
#Override
public void run(){
// non-static method findViewById
ImageView sky = (ImageView)findViewById(R.id.sky);
}
};
}
The problem is that the gc doesnt seem to free the fullAnim thread so that the heap is growing by ~100K at every restart - till it slows down and crashes. Declaring fullAnim as static does solve this problem - but as i use non static references this doesnt work out for me.
So at this point i am kindof lost - and i hope u can advice me where to go next. Is there something i might be doing wrong or is there a tool i can use to manage threads to drop and free heap after restart.
kindly regards
UPDATE
thanks to everyone who answered - helped alot. using TimerTask did the trick in the end. i did the following change:
/****** THREADS AND RUNNABLES ******/
final TimerTask fullAnim = new TimerTask(){
#Override
public void run(){
try{
hand.post(anim1);
Thread.sleep(2000);
hand.post(anim2);
Thread.sleep(1000);
// and so on
}catch(InterruptedException ie){ie.printStackTrace();}
}
};
as the activity was more than 6k loc long this was a pretty decent solution without facing bigger impacts. KUDOS!
i dont use a Timer to shedule the task - dont know if its bad practice but
the animation is called like this:
Thread t = new Thread(fullAnim);
t.start();
A running Thread is never garbage collected.
A Thread is not stopped automatically if your Activity stops or is destroyed. It could run forever.
Every non-static inner class keeps a reference to the enclosing instance. E.g. hand.post(anim1); works inside that inner class because it has an implicit reference to AllocActivity.this.
So what you effectively do is to keep a reference to your Activity alive for longer than it is supposed to be alive, i.e. until after onDestroy.
Make sure to stop threads manually if you don't want them anymore.
Because final variable have low priority for GC. So you need to explicitly release the runneable objects in onPause() method because there is not ensurence onDestory() will call immediate after finish() call .
#Override
protected void onPause(){
super.onPause();
//cancel timer to stop animations
if(t!=null){
t.cancel();
}
System.gc();
}
UPDATE
use timer to achieve this
boolean isFirstAnim=true;
Timer t = new Timer();
t.schedule(new TimerTask() {
#Override
public void run() {
if(isFirstAnim){
// play your first animation at every
}else{
// play your second animation at every
}
}
}, 0, 3000);
What happens when all activities of an application finishes?
"When you call finish() this doesn't mean the Activity instance is
garbage collected. You're telling Android you want to close the
Activity (do not show it anymore). It will still be present until
Android decides to kill the process (and thus terminate the DVM) or
the instance is garbage-collected."
You need to implement your own stop method to stop the running thread, you can make a call to it in onDestroy
refer this Stopping a runnable
Alternatively
you can perform your operation in an asynctask and use onProgressUpdate() to publish progress on UI thread and use cancel(true) in combination with check in doInBackground() whether cancel has been called to stop the task.
I'm designing a music player app for Android that will feature pop-up controls. I'm currently trying to get these controls to close after a certain period of inactivity but there doesn't seem to be a clearly documented method of doing this. So far I have managed to cobble the following solution together using a few suggestions both from this site and others.
private Timer originalTimer = new Timer();
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.playcontrols);
View exitButton = findViewById(R.id.controls_exit_pane);
exitButton.setOnClickListener(this);
View volUpButton = findViewById(R.id.controls_vol_up);
volUpButton.setOnClickListener(this);
View playButton = findViewById(R.id.controls_play);
playButton.setOnClickListener(this);
View volDownButton = findViewById(R.id.controls_vol_down);
volDownButton.setOnClickListener(this);
musicPlayback();
originalTimer.schedule(closeWindow, 5*1000); //Closes activity after 10 seconds of inactivity
}
And the code that should close the window
//Closes activity after 10 seconds of inactivity
public void onUserInteraction(){
closeWindow.cancel(); //not sure if this is required?
originalTimer.cancel();
originalTimer.schedule(closeWindow, 5*1000);
}
private TimerTask closeWindow = new TimerTask() {
#Override
public void run() {
finish();
}
};
The above code makes perfect sense to me but it force closes upon any user interaction. It does however close normally if untouched and won't close after interaction if I remove the second schedule, so this seems to be the problem. Also note that I imagine I will be moving this timing task to another thread to help keep the UI snappy. I need to get it working first though :D. If there's any more info I need to supply please ask and thanks for any help...Ye guys are brilliant!
Based on #CommonsWare's suggestion, switched to a Handler. Works perfectly. Thanks very much!
private final int delayTime = 3000;
private Handler myHandler = new Handler();
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.playcontrols);
View exitButton = findViewById(R.id.controls_exit_pane);
exitButton.setOnClickListener(this);
View volUpButton = findViewById(R.id.controls_vol_up);
volUpButton.setOnClickListener(this);
View playButton = findViewById(R.id.controls_play);
playButton.setOnClickListener(this);
View volDownButton = findViewById(R.id.controls_vol_down);
volDownButton.setOnClickListener(this);
musicPlayback();
myHandler.postDelayed(closeControls, delayTime);
}
and the other methods...
//Closes activity after 10 seconds of inactivity
public void onUserInteraction(){
myHandler.removeCallbacks(closeControls);
myHandler.postDelayed(closeControls, delayTime);
}
private Runnable closeControls = new Runnable() {
public void run() {
finish();
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
};
To complete the answer above, note that the Activity.onUserInteraction() is adequate only if you care about clicks.
The documentation at http://developer.android.com/reference/android/app/Activity.html#onUserInteraction%28%29 states: "Note that this callback will be invoked for the touch down action that begins a touch gesture, but may not be invoked for the touch-moved and touch-up actions that follow."
Actual implementation proved it indeed ignores all movements on the tablet, which means the clock is never reset while, say, drawing without releasing the finger. On the other hand, it also means that the clock is not reset too often, which limits the overhead.