wait() and notify() in a thread android - android

Im writing an educational mathematics game were you can choose many operations at the same time, now im trying to generate 5 questions, however it runs all the five questions soo fast that i cant answer them except last questions because it is stuck there. so i thought about creating a thread that will wait() after creating the first question and wait till it is solved and answered correctly, then proceed to the next question and so on... now i have never used wait and notify before so where should i assign them
here what i got so far, however it gives me an exception:
while (counter < 5) {
Thread pause = new Thread() {
#Override
public void run() {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
} finally{
// ops array is for knowing what operation he chose,
// 1 for plus and 2 for minus
//generate random number within the range of the operations length.
int gen = Integer.valueOf((int) ((Math.random() * (ops.length))));
Log.d("Testgen", String.valueOf(gen));
//random operation is generated
int TheChosenOperation = ops[gen];
Log.d("Test The chosen", String.valueOf(TheChosenOperation));
switch (TheChosenOperation) {
case 1: // if it is plus, assign the generated numbers to i and j from plus.java
Toast t = Toast.makeText(SecondActivity.this, "Plus", 5000);
t.show();
tv.setText("+");
int i = plus.getBorder();
int j = plus.getBorder2();
tv2.setText(String.valueOf(i));
tv3.setText(String.valueOf(j));
answer = plus.getAnswer();
break;
case 2: //if it is minus, assign the generated numbers to i and j from minus.java
Toast t2 = Toast.makeText(SecondActivity.this, "minus", 5000);
t2.show();
tv.setText("-");
int i2 = minus.getBorder();
int j2 = minus.getBorder2();
tv2.setText(String.valueOf(i2));
tv3.setText(String.valueOf(j2));
answer = minus.getAnswer();
break;
}
b.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (answer > 0 && answer == Integer.parseInt(ed.getText().toString())) {
Toast t = Toast.makeText(SecondActivity.this, "true",
5000);
t.show();
this.notify(); // if the answer is true then notify()
} else {
Toast t = Toast.makeText(SecondActivity.this, "false",
5000);
t.show();
}
}
}); // end of clicklistner
counter++; // counting for 5 questions
}//finally
}//run
}; // end of thread
pause.start();
} //end of while loop
im still new with android and threads so please be patient with me.
Thanks in advance and sorry for my horrible english.

The usage of Thread in this context is not really correct.
There's a main thread or the UI thread responsible for presenting and managing the UI state. If you want to make any changes to the UI elements like TextView, Button, etc., you'll have to do it in this thread. If you keep long duration tasks in this thread or make this thread wait, your application will become unresponsive and the Android OS will show an ANR dialog. (Read more about it in Keeping Your App Responsive)
Threads are usually used when there are concurrent tasks to be executed. In this case, the tasks are not really concurrent, but rather sequential.
You should think more in terms of an event-driven approach where the application takes decision based on events (like question answered, which can be used to check how many questions have been answered to decide whether you need to generate more questions).

Related

Best way to update Android-UI in while loop

I'm working on a Tetris-app and I have a Thread.sleep command to animate the falling of the tetriminos. But that creates a conflict with the UI. So I tried runOnUiThread() like this:
while (gameover == false) {
tetriminoSettled = false;
try {
Thread.sleep(1000 - (counter * 3));
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
#Override
public void run() {
Random ran = new Random();
int x = ran.nextInt(7) + 1;
addTetrimino(createTetrimino(x)); //the UI operation
gridView.invalidate();
}
});
But the UI gets only updated after the game ends.
Do you think AsyncTask is a good approach?
Please try to keep in mind that I later need additional UI-Threads for shifting the tetriminos left and right and so on.
Thanks!
Judging from the code you posted, it looks like you are using a gridview for a game. It is possible but not worth the effort. Just use a SurfaceView as shown in this short tutorial. You'll have an onDraw callback in which you can update whatever you like every drawing cycle. Have fun, coding games is really hard :).
Try gridView.postInvalidate();

Updating a chart from an ArrayList over time?

I have an ArrayList of values, and I would like to iterate through the ArrayList. For every new value, I would like to update the chart with that value, and then wait a set amount of time before doing the same thing to the next value.
At the moment, my log says that all of the values are being iterated over. However, on my testing device, the chart does not update until the very end; at that point, all of the values are loaded at once, so there is no desired "slideshow" effect.
When I want to start playing back the values in my ArrayList, this method is called:
public void playback(){
if(ret != null) {
for (int x = 0; x < ret.size(); x++) {
addEntry(ret.get(x));
try {
Thread.sleep(100);
} catch (Exception e){
//Do nothing
}
}
} else {
Log.d(LOG_TAG, "ret was null.");
}
}
What can I do so that the values are displayed on my chart, one after another, with a certain amount of time between each value?
Edit: Here was the solution I ended up implementing with help from Shadab Ansari:
public void playback(){
if(ret != null) {
addEntry(0);
} else {
Log.d(LOG_TAG, "ret was null.");
}
}
private void addEntry(int index) {
final int in = index;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
yVals1.get(0).setVal(ret.get(in).intValue());
RadarDataSet set1 = new RadarDataSet(yVals1, "Set 1");
// And other UI stuff
// Recursive call!
if(in < ret.size() - 1){
addEntry(in + 1);
}
}
}, 100);
}
In case it was not clear, ret was a global variable that contained the arrays that I was going to be inserting. yVals1 was an ArrayList of Entries to populate the radar chart.
The end result is that, in this example code, the chart is updated with the next value in my ArrayList every 100 milliseconds. During this time I can still zoom in/out of the chart and rotate it with no problems.
If your addEntry() performs a UI operation then let me explain your problem -
Explanation -
Android is an event based system. Something happens on the device (the screen is touched, a key is pressed, etc.) and Android raises an event. An App is notified of an event and when one occurs that it needs to respond to it does so, often running the code that you have written. Your App runs its code in a loop under the control of the Android Operating Systems (OS). This code loop is referred to as the App's thread of execution. There is only one thread and it is responsible for both running the App code and updating the display.
So the UI update does not happen immediately and your making the UI thread sleep for 100 ms every time the loop runs. And when Android tries to update the UI, you make the thread sleep which means during this time period UI thread will not do anything. And this happens till your loop finishes. After your loop ends, the final event gets executed and you will see your UI updated by the call of addEntry() with the latest value passed.
Solution -
You can use postDelayed()-
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Perform your task and it will be executed after 100 ms
}
},100);

Make views visible one by one; android

I'm having trouble with making views visible. As in: they do all appear, but at the same time, whereas I would like to show them with some delay in between. Currently I have the following code, which should make it more clear:
public void performExperiment (View v) {
Log.i(TAG, "Experiment has started on view: " + v);
final ArrayList<FocusPoint> permutation = new ArrayList<>(Arrays.asList(focusPoints));
Collections.shuffle(permutation);
for (FocusPoint fp: permutation) {
try {
Thread.sleep(1000);
fp.setVisibility(ImageView.VISIBLE);
//fp.invalidate();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "Sleep failed");
}
}
}
The FocusPoint-class is an extension of ImageView in Android and the method is called upon a button-click.
What needs to happen is that all the views show up in a random order on the screen, with a second between them. This code however waits for 16 seconds (the amount of views is 16) and then shows all the views at once. I tried invalidating my the view to redraw it, and I also tried to take 'fp.setVisibility(ImageView.VISIBLE)' out of the try-catch block, but both didn't work. (Of which the latter obviously doesn't work, I didn't really expect that to work, but I'm getting really desperate :P) I have been searching for hours now, and none of the StackOverflow-pages and other fora/documentation had an answer for this problem. How can I make sure that the focuspoint draws, before the loop continues to the next?
Using Thread.sleep(ms) to delay the UI thread is a very dangerous idea, and as you can see, it doesn't lead anywhere. By blocking the thread for 16 seconds, you are effectively freezing the application for that period - including any redraws and other event handling that might happen during that time. Don't ever do that.
Instead, use a Handler and its postDelayed(Runnable, ms) method to schedule visibility changes in the future. Handlers work by adding messages to the event loop, so they don't disrupt normal behavior.
Check this modified version of your code:
private static final long FP_SHOW_DELAY_MS = 1000;
private Handler handler = new Handler();
public void performExperiment (View v) {
Log.i(TAG, "Experiment has started on view: " + v);
final ArrayList<FocusPoint> permutation = new ArrayList<>(Arrays.asList(focusPoints));
Collections.shuffle(permutation);
for (int i = 0; i < permutation.size(); i++) {
final FocusPoint fp = permutation.get(i);
handler.postDelayed(new Runnable() {
#Override
public void run() {
fp.setVisibility(View.VISIBLE);
}
}, (i + 1) * FP_SHOW_DELAY_MS);
}
}

how to stop a running thread with button toggle (android app)

I finally got my app working, i just have one issue which i would like to correct.
I have a button which controls a thread that runs a couple function in the background. The functions in the background eventually stop the thread whenever a certain value is reached. What i am having issues doing is pressing that same button again to just stop the thread manually. Currently I can only start the thread and wait for itself to finish. I am able to do other things in the app, so the thread is running on its own, i just want to kill it manually.
public void onMonitorClick(final View view){
if (isBLEEnabled()) {
if (!isDeviceConnected()) {
// do nothing
} else if (monitorvis == 0) {
showMonitor();
DebugLogger.v(TAG, "show monitor");
//monitorStop = 4;
Kill.runThread(); // I want a function here that would kill the
// thread below, or is there something that
// can be modified in runThread()?
// I did try Thread.Iteruppted() without luck
shutdownExecutor();
} else if (monitorvis == 1) {
hideMonitor();
DebugLogger.v(TAG, "hide monitor");
monitorStop = 0;
runThread(); //The running thread that works great on its own
}
}
else {
showBLEDialog();
}
}
private void runThread() {
new Thread() {
int i;
public void run() {
while (monitorStop != 3) { //This is where the thread stops itself
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
((ProximityService.ProximityBinder) getService()).getRssi();
rssilevel = ((ProximityService.ProximityBinder) getService()).getRssiValue();
mRSSI.setText(String.valueOf(rssilevel) + "dB");
detectRange(rssilevel);
}
});
Thread.sleep(750);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
On first look, you could simply set monitorStop = 3, which would cause the thread to eventually stop after it's timeout completes.
The problem with this, is that I presume if you push the button again or your code modifies monitorStop at some point in the future, then the thead you wanted dead, might stay alive. ie: monitorStop will need to stay equal to three for at least 750ms to assure the thread will comlete it's loop and die.
The correct way to do this would be to create your thread as a new class with it's own monitorStop parameter. When you create the thread, you would keep a reference to it and modify the thread's monitorStop parameter. This way the thread would finish without interruption. If you wanted to create a new thread, then this would not affect the old thread from finishing appropriately.

How I can use a handler to change the ImageView's picture after 2 sec, and 5?

I have an ImageView, what shows one picture . I tried to use a thread, but it doesn't change the picture. Then I tried a handler, but it doesn't treat the sleep(int) method, so I can't increase the time, what elapsed. How can I make it? Can you write an example please?
Here is my original code:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Thread timer = new Thread() {
public void run() {
int time = 0;
while (time <= 7000) {
try {
sleep(100);
time =+ 100;
if(time == 2000) {
radar.setImageResource(R.drawable.radar_full);
}
if(time == 5000) {
radar.setImageResource(R.drawable.radar_50);
}
if(time == 7000) {
radar.setImageResource(R.drawable.radar_found);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
timer.start();
};
};
Here are a couple of tutorials for you:
Android GUI thread timer sample
A Stitch in Time
It appears that you need to learn more about Threads and Handlers in general. However, it's worth pointing out that you cannot update a UI element from within a Thread, which is what I'm guessing you tried to do; UI updating (such as changing the content of an ImageView) must be done within the UI thread. Therefore, updating the image inside a Handler on the UI thread was going in the right direction. You just need a way to call that Handler at timed intervals, and the tutorial above demonstrates one such way, by simply posting messages to the Handler.

Categories

Resources