Android programming: How do I get out of a while loop? - java

My program should repeat a method until a button is pressed.
I tried this, but is doesn't work:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
public void onClick (View v){
switch(v.getId()) {
case R.id.button1:
mainprogram();
break;
case R.id.button2:
perform = false;
break;
}
}
public void mainprogram(){
while(perform == true){
speak();
}
}
(Of course I programmed the "speak()" method)
Could you tell me where the problem is or if there are any methods to solve it?

The problem is that your loop runs on the UI thread. I guess the whole UI freezes as you start the loop.
You should run it on a separate thread. Like:
case R.id.button1:
Thread th = new Thread(new Runnable(){
public void run(){
mainprogram()
}
}).start();
break;
Also, since you modify "perform" from separate threads, you should also make the perform variable volatile, to make the changes visible to other threads, as soon as the modification happens.
You can read more about volatile here:
Do you ever use the volatile keyword in Java?

I think the problem is that you are running the while loop in the same thread your listeners and your UI is running in. Thus, as soon as your program enters mainprogram(), it will hang up in this loop, because it can no longer react to your UI (thus, perform will always be true)!
The problem you are facing might be solved with running the content of mainprogram() in another Thread.
Your code might look something like this:
class MainActivity extends Activity
{
//...
private static boolean perform;
private static getPerform()
{
return perform;
}
public void onClick (View v){
switch(v.getId()) {
case R.id.button1:
perform = true;
Thread t = new SpeakThread();
t.start();
break;
case R.id.button2:
perform = false;
break;
}
}
}
class SpeakThread extends Thread
{
public void run()
{
while(MainActivity.getPerform())
{
speak();
}
}
}

Since mainprogram will be running on the UI thread, it will block all UI until it breaks from the loop.
Clicking R.id.button2 will not respond (because the UI thread is blocked), so perform will not be set to false.
Perform mainprogram off the main thread, and clicking the button will stop it at some point later

To stop a while loop just use break;
So in your case:
while(perform == true){
speak();
if( ... somereason ...) break;
}
Where ... somereason ... should be valid code.
If the method speak() is the one that decides if you need to break, just put the code of speak() directly in the while loop instead of the method call. Or return false from speak and check:if(!speak()) break;

Related

Thread seems to kill itself after a while?

I'm very new into Threads and I'm facing following problem when using a thread in my code.
On a button click I'm starting a thread which runs a specific task, and this task is running in the background of the system. I am using a while loop inside the thread to check if a volatile bool is changed by another button click to stop the whole process. The problem is I have to add an empty loop else it looks like the thread stops itself and does not check for the while condition anymore. I assume this is very inefficient and wastes a lot of ressources.
I'm adding a shorted version of the code to make it less unreadable.
Any idea why this happens and how I could improve the overall efficiency of my code?
public void onClick(View view) {
new Thread(new Runnable() {
public void run() {
//execute my task
while(!stopThread) {
// Without this empty loop the thread stops after a while
}
while(stopThread) { // stopThread is a volatile bool changed by another button click
//Finish the task
break;
}
}
}).start();
}
I guess that stopThread has the initial value of false, which causes your 2nd loop not to be executed and the threads terminates.
Instead of using a loop you could share some lock object between both buttons, and use wait and notify instead. Check this tutorial.
A possible Implementation could look like this:
Both buttons need access to these two variables:
Object lock = new Object();
volatile boolean stopThread = false;
The onClick method of the first button:
public void onClick(View view) {
new Thread(new Runnable() {
public void run() {
// execute my task
synchronized (lock) {
// stopThread is a volatile boolean changed by another button click
while (stopThread == false) {
lock.wait();
}
}
// Finish the task
}
}).start();
}
The onClick method of the second button:
public void onClick(View view) {
new Thread(new Runnable() {
public void run() {
synchronized (lock) {
stopThread = true;
lock.notify();
}
}
}).start();
}
You will want to keep the boolean flag around to handle spurious wakeups. If the condition is fullfilled you continue with your code, else you go back to wait for the notification.
Note that the example does not deal with any interrupts that may happen.
You might want to use AsyncTask.execute instead of starting Threads on your own.
Check this answer.

Android Thread Allocation - growing heap?

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.

How can I make a piece of code loop while a Button is pressed and stop it once the Button is no longer Pressed?

I am using the android developer tools in Eclipse, programming in Java, and I need to make an object move across the screen as long as a button is pressed. I've been doing research for hours, and I cannot find any methods to accomplish this. I've tried running threads, which often crash or seemingly don't execute. I've also tried an onClickListener which reads the button state and uses it to determine whether or not the button is still pressed. I'm currently using a while loop, but this just freezes the program. I believe that this is the best method, and I've tried to use Thread.sleep in order to limit the number of iterations per second, as I believe that this is the reason it is freezing. Am I on the right track or am I way off in left field? Here is a snippet of code:
rightButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
while(arg0.isPressed())
{
mover.updateCoordinates(1, 0);
}
}
});
Would you try this another method?
Firstly declare your button as class variable, declare a Handler and a Runnable:
private Button rightButton; // You will assign this in onCreate() method
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
#Override
public void run() {
if(rightButton.isPressed())
{
// If press state is pressed, move your item and recall the runnable in 100 milliseconds.
mover.updateCoordinates(1, 0);
mHandler.postDelayed(mRunnable, 100);
}
}
};
Then your button's onClickListener will looks like this:
rightButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0)
{
// Instead of performing a loop here, just call a runnable, do simple press state checking there.
mHandler.postDelayed(mRunnable, 100);
}
});
Create a loop that updates the views, waits, and calls itself after it finishes waiting. Within the loop, have an animation if statement with a boolean field that move on true and does not move on false. Have the onClick method toggle the boolean field's value.
Try:
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
// Start your animation here
} else if (e.getAction() == MotionEvent.ACTION_UP) {
// Stop your animation here
}
return false;
}
});
References:
MotionEvent
OnTouchListener
Alternatively, override onKeyUp and onKeyDown of the View class and check for the KEYCODE_ENTER KeyEvent

Android: Wait() the main thread while a dialog gets input in a separate Thread

I'm writing an activity in Android where the user modifies an SQL Database. The UI consists of an EditText where the user enters the name, and a Seekbar where the user enters how attractive the person is. Underneath there are a bunch of buttons: add, edit, view, delete.
When the user clicks on the "Edit" button, an input dialog is displayed asking the user to input the record number. Once that is done, that record is loaded.
The problem I was having was that the inputdialog would be displayed and while the user entering the record no, the rest of the edit method would carry on so that by the time the user was done entering the input - nothing happened because the function had already been completed.
In order to solve this problem, I decided to use Multi-Threads (which I do not have much experience using). When the edit button is pressed, the main UI Thread is blocked (using wait() - this is because I don't want the UI to be active while the user is entering the record id.) and the input dialog is displayed in a seperate thread.
Once the input has been entered, the thread is notified and the rest of the edit function continues. (The code is below).
The problem is that when I call the wait function on the UI Thread, I get an error that says "object not locked by thread before wait() ". How do I lock the UI Thread?
I know in general one shouldn't block the UI Thread, but I think it's okay in this case because I don't want it to accept any user input.
Thanks for your help.
public class Attractivometer extends Activity implements OnClickListener {
private Button buttonAddRecord, buttonEditRecord, buttonSaveChanges;
private Button buttonDeleteRecord, buttonViewRecord;
private EditText fieldName;
private SeekBar seekbarAttractiveness;
private String inputFromInputDialog=null;
private Thread inputThread;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.Attractivometer);
buttonAddRecord = (Button) findViewById(R.id.buttonAddRecord);
buttonSaveChanges = (Button) findViewById(R.id.buttonSaveChanges);
buttonEditRecord = (Button) findViewById(R.id.buttonEditRecord);
buttonDeleteRecord = (Button) findViewById(R.id.buttonDeleteRecord);
buttonViewRecord = (Button) findViewById(R.id.buttonViewRecord);
fieldName = (EditText) findViewById(R.id.fieldName);
seekbarAttractiveness = (SeekBar) findViewById(R.id.seekbarAttractiveness);
buttonAddRecord.setOnClickListener(this);
buttonSaveChanges.setOnClickListener(this);
buttonEditRecord.setOnClickListener(this);
buttonDeleteRecord.setOnClickListener(this);
buttonViewRecord.setOnClickListener(this);
}
public void onClick(View clickedItem)
{
switch(clickedItem.getId())
{
case R.id.buttonAddRecord:
//.....
break;
case R.id.buttonSaveChanges:
//...
break;
case R.id.buttonEditRecord:
inputThread = new Thread(new Runnable(){
public void run()
{
showInputDialog("Enter Record ID", InputType.TYPE_CLASS_NUMBER);
}
});
inputThread.start();
try {
Thread.currentThread().wait();
} catch (InterruptedException e) {
Log.e("Attractivometer","Main Thread interrupted while waiting");
e.printStackTrace();
}
try {
inputThread.join();
} catch (InterruptedException e) {
Log.e("Attractivometer","Input Thread interrupted while joining");
e.printStackTrace();
}
int recordId = Integer.parseInt(inputFromInputDialog);
if(recordId!=null)
{
AttractivometerSQLHandler AttractivometerDatabaseHandler = new AttractivometerSQLHandler(this);
AttractivometerDatabaseHandler.openDatabase();
String recordName = AttractivometerDatabaseHandler.getName(recordId);
String recordAttractiveness = AttractivometerDatabaseHandler.getAttractiveness(recordId);
if(recordName==null || recordAttractiveness==null )
{
//no record found.
Toast.makeText(this, "No record with that ID found", Toast.LENGTH_SHORT).show();
}else
{
fieldName.setText(recordName);
seekbarAttractiveness.setProgress( Integer.parseInt(recordAttractiveness) );
recordIsOpen(true);
}
AttractivometerDatabaseHandler.closeDatabase();
}else
//No input.
recordIsOpen(false);
break;
case R.id.buttonDeleteRecord:
//...
break;
case R.id.buttonViewRecord:
//....
}
}
private void showInputDialog(String prompt, int inputType)
{
AlertDialog.Builder inputDialog = new AlertDialog.Builder(this);
inputDialog.setTitle("Record No.");
final EditText fieldInput = new EditText(this);
fieldInput.setInputType(inputType);
fieldInput.setHint(prompt);
inputDialog.setView(fieldInput);
inputDialog.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
inputFromInputDialog = fieldInput.getText().toString();
inputThread.notify();
}
});
inputDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
inputFromInputDialog = null;
inputThread.notify();
}
});
inputDialog.show();
}
}
No, no, no! Do not block the UI thread. The system will raise an "Application Not Responding" error. Also, do not try to interact with the user from a non-UI thread.
When the user clicks "edit", don't start the edit method. Just pop up a dialog to collect the required information. Add a DialogInterface.OnClickListener to the positive button and (with the required information now in hand) start the edit method from there.
See the guide topic Dialogs for more information.
First of all, you should not pause the main Thread, because everything will be frozen if you do that, and from an user perspective, that is not good at all.
And second, you should pop up a dialog with 2 buttons, (Done, Cancel) and allow user to go further by pressing one of those buttons. Here you can find out how to display a custom dialog: http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application
First rule of multi-thread programming in Android is that, You should never stop UI thread, and all the long running operations should be made in separate thread. And by long running operations I mean, SQLite database writing/ reading etc.
before invoke wait need get the object lock like this:
synchronized(Thread.currentThread()) { //added
try {
Thread.currentThread().wait();
} catch (InterruptedException e) {
Log.e("Attractivometer","Main Thread interrupted while waiting");
e.printStackTrace();
}
}
but why you want to make main thread wait?

Does the onClickListener not update the UI until all of the code inside it has run in Android?

I have an onClickListener that triggers a network call so I would like to have some way to show the user that communications are in progress. The problem I am running into is that I can't seem to throw up a ProgressDialog or change the UI in any way for that matter before the call is made inside the onClick Listener. All of the code works just fine, but the UI changes don't come into effect until after all the code in onClickListener runs.
I was wondering if my problem is simply that an anonymous inner class like an onclicklistener can only update the UI at the end of its run? Or maybe my code is just bad.
Thanks in advance
Below is the code for the onclick Listener :
relayButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cPanel.throwProgress(NCDTCPRelayActivity.this);
System.out.println(tvSocketConnection.getText().toString());
if (relayStatusArray[relayNumber] == 0)
{
if (cPanel.TurnOnRelay(relayNumber, 1) == false)
{
changeTitleToRed();
}else{
changeTitleToGreen();
}
}
else {
if (cPanel.TurnOffRelay(relayNumber, 1) == false){
changeTitleToRed();
}else{
changeTitleToGreen();
}
}
cPanel.hideProgress(NCDTCPRelayActivity.this);
}
});
Here is the code for the throwProgress and hideProgress respectively (these are in a subclass of the activity):
public boolean throwProgress(Context mContext) {
System.out.println("INSIDE THROWPROGRESS");
try {
tempDialog = ProgressDialog.show(mContext, "Connecting", "Connecting", true);
}
catch (RuntimeException e) {
return false;
}
return true;
}
public boolean hideProgress(Context mContext) {
System.out.println("OUTSIDE THROWPROGRESS");
tempDialog.hide();
return true;
}
**Edit
Here is the new code for the onClickListener that I put the runnable in:
public void onClick(View v) {
cPanel.throwProgress(NCDTCPRelayActivity.this);
System.out.println(tvSocketConnection.getText().toString());
handler.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
if (relayStatusArray[relayNumber] == 0)
{
if (cPanel.TurnOnRelay(relayNumber, 1) == false)
{
changeTitleToRed();
}else{
changeTitleToGreen();
}
}
else {
if (cPanel.TurnOffRelay(relayNumber, 1) == false){
changeTitleToRed();
}else{
changeTitleToGreen();
}
}
cPanel.hideProgress(NCDTCPRelayActivity.this);
relayStatusArray = cPanel.getBankStatus(1);
updateButtonText();
}
});
}
Changing UI from your click handler should work just fine. The problem is likely that you're doing some heavy work on the UI thread and it's blocking it so that the dialog is not really updated until after all of that work is done. Try moving all the heavy lifting into an AsyncTask (read this doc if you're unfamiliar with it) hiding the dialog when the task is complete and see if that fixes it.
All the UI updates are delayed on Android, as well as on pretty much every GUI platform out there. Changes to the looks of a view are never rendered right away; instead, the GUI subsystem marks the view as "needs redraw", and calls draw() some time later on the message loop.
If you want something to take place after the screen has been updated, use Handler.post(). The post()'ed code will execute some time on the message loop, typically later than the queued draw code.
Aside node: Windows GUI is a happy exception to that rule; you can draw on a Windows window outside WM_PAINT. But on Android you can't.

Categories