TL;DR:
How can I give one intent a preference over another?
If two intents are run at the same time, I want only one of those intents to start.
My game has a 20 second timer running in the background service, and when that timer ends, a new activity (game over screen) starts through an intent. The user is pressing buttons to change activities (again, through intent) in those 20 seconds.
The problem is, if the user pushes a button at the same time that the timer ends, the next activity starts rather than the game over screen. Now, the timer is over, and the user is stuck randomly pushing buttons for no reason. How can I fix this? Is there any way to make sure that the Game Over screen will open up after 20 seconds?
I haven't provided much code, because it is mainly just starting intents. If you need any detail, feel free to ask me.
do timer checking on user click event should do it. or you can use flag/boolean to maintain state.
i dont know your code, just use simple conditional if..
if (timer <= 0){
//open whatever u want and reset timer again..
}
I am suggesting you to take a global variable as a flag,
let say int = 0
Now initialize it as "0" and on your both intents give condition as below:
if(i == "0"){
.
.
.
//fire your intent..
i= 1;
}
,And then flush that variable to "0" whenever you want.Hope you got idea.
Related
How to create a timer runed in the background and display one second of a second in the TextView dynamically in MainActivity.Whatever I exit app or leaving the MainActivity,the timer also keep on.
I thinked that I can use service and BroadcastReceiver or using Handler and Thread.But I can't solve it.
Having an always-on service to simply count up seconds is a bad idea, it'll waste battery life and could be killed at any time.
What you could do is something like this:
Make a counter that you can start/stop with a button.
On your your activity's onPause, save the System.currentTimeInMillis() along with the current count on your timer
When you resume your activity, use the current System.currentTimeInMillis() value to calculate what your timer should be displaying had it been really running all that time.
I have thinked a solution; Thread+Handler; EveryTime enter ther MainActivity
we just set the time interval = EndTime-System.currentTimeMillis();
I am writing an app that say has two activities A & B. A is a display page while B is a settings page.
In runtime, there is a timer loop running in A which keep on updating the display. And the settings in B affects some tasks in the timer loop.
I want it possible for users to switch from A to B in runtime and make changes in B, while the timer loop is still running in A and will response to the changes made in B.
Is it possible?
Or should I place the timer loop in somewhere else?
I'm new in Android & Java. Please kindly help. Thx.
I am revoking a timer in android using intent now if i want to put that timer in a background when user clicks on directly back button then how do i do that? and then when i see background apps running it should be working and can be brought to front.
The problem is that if you define the counter in your Activity, the counter is bound to the Activity life-cycle. In order to workaround this, you can create your counter in a Service and just visualize the value in Activity.
There are also several techniques which you can use when defining the counter inside the Activity. Using SystemClock to track the time in your Activity in onStart() and onStop() for example, you can manually calculate the difference between those two values and adjust accordingly your counter.
Actually i just want to know whether can we set timer when the activity changes.
Detail description:
startActivity(new Intent(this,Second.class));
I know that by using this code we can change one activity to another activity. By the above code when the activity is changing it changes quickly as know to everyone, but what i want is in button click event when i write this code, when i click that button the same activity needs to be on screen for some particular time (i.e., around 10 seconds) & after that it needs to change activity. I thought of keeping timer here but i didn't got any idea how to do that.
Can anyone please help me with this.
The easiest thing would be to simply create a Handler and post a message to it 10 seconds later.
Handler activityChanger = new Handler();
activityChanger.postDelayed(new Runnable(){
startActivity(new Intent(this,Second.class));
}, 10000);
Put that where you normally create your activity within the scope of startActivity. You're current activity should run for 10 more seconds and the new one starts up.
I would like to run some action when all activities of certain type X are closed. The first idea is to have some global counter, that is decremented each time activity X is closed, and once the counter is 0, run some action. Assuming that process can be killed, the counter have to be persisted. But one more issue remains - imagine that activity X can crash (due to some bug in code), and in this case I'll not decrement the counter.
Any idea how to implement it in robust way?
I am not sure what you mean by 'closed', but you should be able to use the Activity Lifecycle callbacks to accomplish what you are asking.
There is a diagram on the following page that shows how the Activity Lifecycle is implemented:
http://developer.android.com/reference/android/app/Activity.html
As you said, you can have a global counter for this.
Maybe the good place for it is in the Application class?
Or u just save you counter in a file and reset it every time you start your application (in a very first activity).
You can also try to save it in shared preferences.
But there is still the problem with crashes. I have just an idea. There is ACRA-library for crash-logging. Take a look in it, how do they catch the crashes.
So you can just write a method for decrementing your counter. And call it from onDestroy() or if a crash occurs.