Im doing a little app, its a memory game, you choose one card, it turns up, you choose the second card, it turns up, if they are the same they are out of the game, if they dont match, they are turned down again.
I have
public class PlayActivity extends Activity implements OnClickListener.
The flip events are trigged by click handlers, declared at public void onCreate(Bundle savedInstanceState) they work fine.
When the first card is selected, it calls my method Action, this sets the image from default (the card back) to the 'real' image (the card front). Fine so far.
My problem is the second card: when its selected, it calls method Action, where it should set the front image (lets call it 'middle action'), then a litle pause (a while loop doing nothing until x milliseconds), and then it checks what to do (if they match or not) and turn them down or take the out of the game. You can see where is the problem: the screen only displays the result after x milliseconds (the 'middle action' is not being draw).
Since I have done some little games with XNA, I know the loop Update-Draw, so I know here im updating the same thing twice so always the last one is drawn. But here, the only updating I can have is when click events are trigged, I need a periodic, constant update.
Help?
You can probably use a TimerTask in order to handle that. You can implement it like the following.
This probably isn't the most robust way to do it, but it is an idea. If I figure out a better way to do it in a short time I'll edit my post. :)
Also I would like to add that if you want to make a game that uses an update / draw loop you may need to use a SurfaceView to draw your game. Look at the example here http://developer.android.com/resources/samples/JetBoy/index.html
public class TestGameActivity extends Activity {
/* UIHandler prevents exceptions from
performing UI logic on a non-UI thread */
private static final int MESSAGE_HIDE_CARD = 0;
private class UIHandler extends Handler {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_HIDE_CARD:
ImageView cardView = (ImageView) msg.obj;
cardView.setImageResource(R.drawable.faceDownCard);
break;
}
}
}
private UIHandler handler = new UIHandler();
// Handle my click. V is the card view
public void onClick(View v) {
final int viewID = v.getId();
// Create a hide task
TimerTask hideTask = new TimerTask() {
#Override
public void run() {
// Construct a message so you won't get an exception
Message msg = new Message();
msg.what = MESSAGE_HIDE_CARD;
msg.obj = findViewById(viewID);
handler.sendMessage(msg);
}
};
// Schedule the task for 2 seconds
new Timer().schedule(hideTask, 2000);
}
}
Related
The following code is from Head First Android. It is for a stopwatch app.
I have a few questions in the following code:
The code runs like -> OnCreate -> runTimer() (skips handler.post()) -> OnStart -> onResume -> Comes back to handler.post().
Why does it skip hander.post() in the first place?
I have two textView.setText(). But the first one doesn't work. It's always the last one. I put the second one just to see what the code does after postDelay() method.
Why doesn't the first one work? I am expecting the text to jump back and forth from "hello" to "hh:mm:ss".
So what exactly happens during the 1-second delay after postdelay() is executed.
Does the code starts running normally and when its 1 second the postDelay() is called?
why is this used in postDealy(this, 100). shouldn't it be this.run()?
public class MainActivity extends AppCompatActivity {
private boolean running = false;
private int counter = 0;
private Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
runTimer();
}
public void onClickStart(View view){
running = true;
}
public void runTimer(){
final TextView textView = findViewById(R.id.timer);
handler.post(new Runnable() {
#Override
public void run() {
int hours = counter/3600;
int minutes = (counter%3600)/60;
int secs = counter%60;
String time = String.format("%d:%02d:%02d", hours, minutes, secs);
textView.setText(time); // Doesn't set it to this - see last line
if(running){
counter++;
}
handler.postDelayed(this,1000); // what does happens between next one second
textView.setText("hell0"); // Always set it to this
}
});
}
Why does it skip hander.post() in the first place?
It is not skipped, it will be executed after onResume() returns. All the Runnables, queued though a Handler associated with the main thread, start their execution only after onResume() returns.
Why doesn't the first one work?
It does work. You just can't visually see it because the two method calls, textView.setText(), are invoked "almost" at the same time.
The following sequence of calls happen at each run():
textView.setText(time),
the same Runnable is posted to the queue with handler.postDelayed(this,1000). Immediately after that
textView.setText("hell0") is called
Why doesn't the first one work? I am expecting the text to jump back and forth from "hello" to "hh:mm:ss".
You should implement an extra logic to switch between time and "hell0" at each run() execution.
E.g. create a boolean flag in the Activity and set either time or "hell0" depending on the flag value (don't forget to change the flag value at each run() execution).
why is this used in postDelay(this, 100). shouldn't it be this.run()?
No, this.run() is executed synchronously (and immediately) and is of type void. The code won't compile as postDelay() expects the Runnable type, not void.
handler.postDelayed(this,1000);
This used to run your function after 1 second. It is a delay for 1 second.
The code written in your handler will execute after a second. That's all.
I have an Android app that needs to display images and videos in a loop, it has no problem with playing the videos, but when it gets to the images the screen goes blank and nothing happens. the program is still running just not displaying.
I'm using SurfaceView.setBackground() to draw the image because swapping between an ImageView and the SurfaceView that the videos are played on has caused problems before.
I was having a similar issue when displaying the videos and I solved it by removing the loop I had waiting for the video to be prepared so ideally I would like to remove the loop I have waiting for a timer to elapse.
I have a function to display the new media when the old one is finished and it also prepares the next one for when the current one is finished. If the media is a video this is called from the onComplete function which works fine. But if it is an image I have a wait loop.
What I want to do is have something like the MediaPlayer.onComplete() function to be called when the image has been displayed for the desired amount of time. Does anything like this exist?
I have since changed this to use handlers because of some other error checking I needed to do. However the other way does work
mHandler.postdelayed(run, delay);
Runnable run = new Runnable(){
public void run()
{
//run function
}
};
I figured it out. I made a new thread that ran a timer and calls a function in the main activity that updates the screen. The call to the function that updates the screen is called using runOnUiThread() so that it has permission to change the screen.
public class ImageTimer implements Runnable{
private long dur, cur,start;
private boolean needed, run;
private Activity mAct;
ImageTimer(boolean running, long duration, Activity act)
{
run = running;
dur = duration;
needed = false;
mAct = act;
}
#Override
public void run()
{
while(run)
{
if(needed)
{
start = System.currentTimeMillis();
cur = System.currentTimeMillis() - start;
while(cur < dur)
{
cur = System.currentTimeMillis() - start;
}
needed = false ;
mAct.runOnUiThread(new Runnable()
{
public void run()
{
MainActivity.ImageDone();
}
});
}
}
}
I use functions for canvas like drawCircle and drawPoint in android.
This works fine.
But the problem now is to draw these different items with a delay, so it looks like an animation.
What kind of mechanism should I use? Have tried with async but I dont like that way of doing it.
Should I use some kind of timer that just draw with an interval or is there other clever ways to do this?
I use this strategy, first I declare a Handler and a Runnable that way:
private final Observable mObservable = new Observable();
private final static int TIME_STEP_MS = 5;
private final Handler mHandler = new Handler();
private final Runnable mTimeManager = new Runnable()
{
public void run()
{
mObservable.notifyObservers(TIME_STEP_MS);
mHandler.postDelayed(mTimeManager, TIME_STEP_MS);
}
};
Then when I want to start my time manager I just call the mTimeManager.run() and it will start to notify my Observer s (previously added) periodically.
If you need for some reason stop the timer or something you just do that:
mHandler.removeCallbacks(mTimeManager);
[ EDIT - More complete code ]
Ok than let's make it clearer, first I made a custom Observable object like that [that's optional]:
private final Observable mObservable = new Observable()
{
public void notifyObservers()
{
setChanged();
super.notifyObservers();
};
#Override
public void notifyObservers(Object data)
{
setChanged();
super.notifyObservers(data);
};
};
the reason for that is just because I can't call setChanged() outside Observable class - it's protected, if it's not changed it doesn't notify any observer.
The other declarations keep the same as shown before, now I need to start this TimeManager somewhere, my app is a LiveWallpaper and I make all rendering stuff into a class that extends a Thread but you don't need that necessarily, I made a method called resumeDrawing(), this one is called right after super.start(); at my #Override of public synchronized void start() from Thread class, the method looks like that:
public void resumeDrawing()
{
if (!mTimeManagerRunning) // just a boolean field in my class
{
System.err.println("Resuming renderer."); // just for debug
mTimeManager.run();
mTimeManagerRunning = true;
}
else
{
System.err.println("Renderer already running."); // just for debug
}
}
and it's dual:
public void pauseDrawing()
{
if (mTimeManagerRunning)
{
System.err.println("Pausing renderer.");
mHandler.removeCallbacks(mTimeManager);
mTimeManagerRunning = false;
}
else
{
System.err.println("Renderer already paused.");
}
}
Ok, now we can start and stop the time manager, but who's listening? Nobody! so let's add'em: On the constructor of my Renderer I add some Observer s to my mObservable object, one of those is the Renderer itself, so my renderer extends Thread and implements Observer:
#Override // from Observer interface
public void update(Observable arg0, Object arg1)
{
mElapsedMsRedraw += (Integer) arg1;
if (mElapsedMsRedraw >= mDrawingMsPerFrame)
{
mElapsedMsRedraw = 0;
drawEm(); // refresh the canvas and stuff
}
}
to add observers you simply do mObservable.addObserver(THE_OBJECT - Implements Observer)
you can see that I don't re-render my stuff each time I'm notified, that's because I use this TimeManager for other thinks than just refresh the Canvas like updating the position of the objects I want to draw just internally.
So, what you need to slow down the drawing is to change the way your objects change internally while the time passes, I mean your circles and points etc, or you can chance your time step, I recommend the first one.
Was it clearer? I hope it helps.
I would use a timer, or create Animations. You can create Animations that will do all sorts of things including changing transparency over time.
Here's the Android Documentation for Animation Resources
I believe there may be sophisticated ways of doing this, but for my needs I used a simple method that has a lot of advantages:
I first create records of coordinates (and any other data needed) for every point of the drawing -- instead of drawing the points on the spot -- and then reproduce them using a timer (Android handler, preferably). This also offers a lot of possibilities while actual drawing: pause, go faster/slower, go backwards, ...
I don't know if this method can be used for complicated drawings, but it is fine for drawing shapes, curves, surfaces, etc.
I'm trying to figure out threading and have this issue where a TextView stops being updated if the app is sent to the back, and then restored.
How can I ensure that the TextView continues to be updated after the app is brought back to the front?
Or...
How do I reconnect the TextView to the handler in my run-nable thread after restarting the activity?
There is a Progress Bar which works just fine, so I'm somewhat confused. I'd appreciate some advice as I think I may be making a simple mistake.
public class ThreadTestActivity extends Activity {
private Handler handler;
private static ProgressBar progress;
private TextView tv;
private int counter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ThreadTest);
handler = new Handler();
progress = (ProgressBar) findViewById(R.id.progressBar1);
tv = (TextView) findViewById(R.id.myText);
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
thread_fun();
});}
}
private void thread_fun() {
new Thread(new Runnable() {
public void run() {
try {
while (counter < 100) {
counter += 20;
Thread.sleep(2000);
// Update the progress bar and TextView (in 5 chunks of 20, 0 to 100)
// This works perfectly it the app stays in front
handler.post(new Runnable() {
public void run() {
// but after sending to the back (esc) and bringing the activity back to the front
progress.setProgress(counter); //This progres bar maintains its value and updates correctly
tv.setText(String.valueOf(counter)); //This TextView reverts to its default text and does not update
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
When an Activity is backgrounded and then resumed, it goes through the whole lifecycle. This means a new instance is created, and Activity.onCreate is called again. See the diagram here for details. An important thing to note is that Activities go through this lifecycle and are destroyed and re-created also when the orientation is changed. So it's something you kind of have to be aware of and work with or around.
The reason that your ProgressBar is updated when your Activity is backgrounded and resumed is that you've got a static reference in your Activity that is set upon each call to onCreate. This static reference is shared between the first instance of your Activity and the second instance that is created after you resume. Once you start your thread, it will just update whatever ProgressBar this reference points to, so you get the impression that the same ProgressBar is being updated.
In fact, it's not the same object, it's a different instance. That's why the text in your TextView is not updated, because the thread is updating an older instance of TextView, not the one on the screen. You can confirm all this in the debugger, or more simply just by printing out the hash codes of the objects. Try putting this inside your while loop:
Log.d("thread_fun", "threadid="+Thread.currentThread().getId()+
",progressbar="+progress.hashCode()+
",textview="+tv.hashCode());
Notice that the ProgressBar's hash code changes, because the thread is updating a new ProgressBar, but the hash code of the TextView that is being updated does not.
D/thread_fun(28989): thread=3483,progress=1097438768,textview=1097437160
D/thread_fun(28989): thread=3483,progress=1097438768,textview=1097437160
D/thread_fun(28989): thread=3483,progress=1097528568,textview=1097437160
D/thread_fun(28989): thread=3483,progress=1097528568,textview=1097437160
Now, the answer is not to make the TextView also static. This isn't how you should maintain state between the Activity lifecycle changes, because it can lead to memory leaks if you're not careful, and frankly it's also confusing, as you can see from above. Instead, you should override one of the other lifecycle methods, onPause or onStop, to save the state of the ProgressBar and TextView, and probably kill the thread that is updating it. Then, in onCreate, you'll need to pull that state out of the Bundle and restore the ProgressBar and TextView to the state they were when the user navigated away, and also probably restart the thread. The method you probably want to read up on is called Activity.onSaveInstanceState. There are other options too, but this is something that's covered on StackOverflow in a lot of questions and is described well in the Android SDK docs.
I hope that helps! Let me know if this isn't clear!
I am having 10 different buttons in my application for different task to perform. I want to develop one service which continuously check (listens) and if user is not clicking any button for particular time let say for 5sec than i wish to perform some other task. How can I check that user has not clicked any button? If anyone having any idea please kindly let me know.
You could simply set a Timer to the desired length. When a button is clicked, just reset the timer. Start the timer in onResume so it starts even if the user is coming back from a phone call or other activity. You should probably stop the timer in onPause of the activity too.
In each of your click listeners save off the time the last button was clicked:
private long lastClickTimestamp;
private Handler handler = new Handler();
public void onCreate( Bundle saved ) {
BackgroundJob job = new BackgroundJob();
handler.postDelayed( job, SECONDS_TO_WAIT * 1000 );
button1.setClickListener( new OnClickListener() {
public void onClick( View view ) {
lastClickTimestamp = System.currentTimeInMillis();
// do the listener logic for button 1 here.
}
});
button2.setClickListner( new OnClickListener() {
public void onClick( View view ) {
lastClickTimestamp = System.currentTimeInMillis();
// do the listener logic for button 2 here.
}
});
// repeat that for all 10 buttons.
}
Now the smarter developer would create a reusable base class that handled setting the timestamp once, then reuse that base class in each of the 10 buttons. But, that's left up to you. Then the background job would look like:
public class BackgroundJob implements Runnable {
private boolean done = false;
// meanwhile in job:
public void run() {
if( lastClickTimestamp > 0 && System.currentTimeInMillis() - lastClickTimestamp > SECONDS_TO_WAIT * 1000 ) {
// let's do that job!
}
if( !done ) {
// reschedule us to continue working
handler.postDelayed( this, SECONDS_TO_WAIT * 1000 );
}
}
}
If you have to use a service you can send a notification to the service saying a button was clicked, then the service can keep track of the time when that occurred. I wouldn't use a service for this because playing an animation or sound doesn't need to survive if the app is put into the background or killed. Services are meant for things like playing music when someone is doing something else, chat applications, or things that need to run in the background when the user isn't interacting with the application. What you're describing could be done as I've shown because when the user gets a phone call or text message they'll leave your application, and the animation or sound you're playing probably should stop too. Pretty easy to do with the Handler option I showed. More difficult, but doable, with a service.
On each button click, update some Calendar object to a new Calendar instance and then you can check what the time is of that Calendar and see if it's more than 5 minutes ago.
//this is a field
Calendar calendar;
public void onClick(View v) {
calendar = Calendar.getInstance();
//etc
switch(v.getId()) {
}
}