Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

How to stop handler or thread

0

61 views

My program is random string First i press button for start and then it will random Second i press same button again for it stop how can i do??

my code

package com.Randomsentence;
import java.util.Random;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Randomsentence extends Activity {
  protected static final int CONTINUE = 0;
  protected static final int STOP = 0;
  TextView txt;
  int time = 30;
  int random;
  public String[] myString;
  Button bt1;
  boolean check = false;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    txt=(TextView)findViewById(R.id.txt);
    bt1 = (Button)findViewById(R.id.bt1);
    bt1.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        thread.start();
      }
    });
  }

  // our handler
  Handler handler = new Handler() {
    public void handleMessage(Message msg) {//display each item in a single line
      {
        if (check == false) {
          Random rgenerator = new Random();
          Resources res = getResources();
          myString = res.getStringArray(R.array.myArray);
          String q = myString[rgenerator.nextInt(myString.length)];
          txt.setText(q);
        }
      }
    }
  };

  Thread thread = new Thread() {
    @Override
    public void run() {
      try {
        while(true) {
          sleep(1000);
          handler.sendMessage(handler.obtainMessage());
        }
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  };
}

this is XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout android:id="@+id/relativeLayout1" android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_marginBottom="40px" android:layout_marginLeft="40px" android:layout_marginRight="40px" android:layout_marginTop="40px">
<TextView android:layout_height="wrap_content"
android:layout_alignParentLeft="true" android:text="TextView"
android:id="@+id/txt" android:layout_width="fill_parent"
android:layout_marginRight="40px"
android:layout_marginTop="50px"></TextView>
<Button android:text="Button" android:layout_height="wrap_content"
android:id="@+id/bt1" android:layout_width="wrap_content"
android:layout_below="@+id/txt" android:layout_alignLeft="@+id/txt"
android:layout_alignRight="@+id/txt" android:layout_marginLeft="40px"
android:layout_marginRight="40px" android:layout_marginTop="40px"></Button>
</RelativeLayout>
</LinearLayout>

This is array.xml insert in to folder values

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="myArray">
<item>ข้าว</item>
<item>ข้าว</item>
<item>เส้นใหญ่</item>
<item>เส้นเล็ก</item>
<item>วุ้นเส้น</item>
<item>ข้าวผัด</item>
<item>เส้นใหญ่</item>
<item>ข้าว</item>
<item>มักกะโรนี</item>
<item>ผัดผัก</item>
<item>ต้มยำ</item>
<item>ทอดกระเทียม</item>
<item>ผัดพริกเผา</item>
<item>ผัดกระเพรา</item>
<item>ผัดน้ำมันหอย</item>
<item>ต้มยำแห้ง</item>
<item>ทอดกระเทียม</item>
<item>แพนง</item>
<item>ผัดกระเพรา</item>
</string-array>
</resources> 

asked June 24, 2011 8:49 am CDT
posted via StackOverflow

2 Answers

1
 

Actually, it's not recommended to stop threads, let the OS do it for you. You should use a flag variable instead of true in your while loop and change it to false when you want to stop your thread. And about Handlers - they cannot be stopped, since they're simply designed to synchronize threads, so don't worry about them.

answered June 24, 2011 9:23 am CDT
0
 

you have to use some flag, which will determine when to setRandom when not initilize boolean variable

boolean showRandom = false;

on button click set its value as follows

something like the folowing

 @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
    //          tv.setHint(tv.getHint());
    //          while(counter<2){
                showRandom = !showRandom;
                System.out.println("..show random.."+showRandom);
                t = new Thread() {
                    public void run() {
    //                  register();
                        try {
                            while(showRandom){
                                System.out.println("..print dialog..");
   sleep(1000);
handler.sendMessage(handler.obtainMessage());


    //                        }
                            }

                }catch(Exception ex){
                    ex.printStackTrace();
                }
                    }
                };
                t.start();

                }

    //      }
        });

answered June 24, 2011 9:23 am CDT

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions
Htaccess handler
January 6, 2011
Htaccess and PHP handler
February 15, 2011