2

My problem is... I have 2 events.. onCreate and Onclick. I have a thread running inside onCreate and i have to stop it inside onClick. Is this possible? If yes, please provide me with some hints/ code snippets how to implement this.

My code inside onCreate event is this:-

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen);

    button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(this);
        try{
            bright = Settings.System.getInt(getContentResolver(), 
                     Settings.System.SCREEN_BRIGHTNESS);
            }catch(Exception ex){
                ex.printStackTrace();
                bright = 1.0f;
            }


        lp = getWindow().getAttributes();
         new Thread(new Runnable(){

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(10000);
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            lp.screenBrightness= dim;
                            getWindow().setAttributes(lp);
                            // SLEEP 2 SECONDS HERE ...
                            final Handler handler = new Handler(); 
                            Timer t = new Timer(); 

                            t.schedule(new TimerTask() { 
                                    public void run() { 
                                            handler.post(new Runnable() { 
                                                    public void run() { 
                                                    lp.screenBrightness=bright;
                                                    getWindow().setAttributes(lp);
                                                    } 
                                            }); 
                                    } 
                            }, 2000); 

                        }
                    });
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }

        }

      }).start();  

    }

this activity is running inside a loop. And i want to stop this wen i click on the button. It seems that i cannot call the Thread.interrupt() method inside the button onclick event. How do i proceed with this?

Goo
  • 1,318
  • 1
  • 13
  • 31
newbee
  • 409
  • 2
  • 12
  • 34
  • [android best and safe way to stop thread](http://stackoverflow.com/questions/8505707/android-best-and-safe-way-to-stop-thread) – Paresh Mayani Feb 22 '13 at 11:59
  • @Bhuro: i've updated my question. thanks for the link anyways – newbee Feb 22 '13 at 12:02
  • FWIW, creating a Handler and a Timer and a TimerTask seems like overkill. Could omit Timer and TimerTask, just do `handler.postDelayed(new Runnable() {..}, 2000);`? – ToolmakerSteve Nov 07 '14 at 03:01

4 Answers4

1

If you create a reference you can access it from everywhere in you class. So, in the class body ad something like

Thread thrd 

Just like you would do so with a Button, TextView, or whatever. Then in the onCreate method add the reference like

thrd = new Thread(new Runnable(){

Now you have a reference to your thread you can use everywhere in your class.

Raymond P
  • 752
  • 2
  • 15
  • 27
  • when i try this myThread = new Thread(new Runnable(){, i get this message "Type mismatch: cannot convert from void to Thread" – newbee Feb 22 '13 at 12:10
1
  1. Declare an boolean mRunnning in your class, this is like a flag, and custom running thread will depend on this to know if it has been cancelled.

  2. As usual, you have a loop in your thread, instead of while(true) do while(mRunning).

  3. Also, you are making your thread sleep, during sleep, thread won't be able to check on mRunning variable, you will have to call interrupt() on thread object to make it stop.

  4. It will be better to use a variable to hold thread reference Thread t = new Thread(....

  5. So, now to stop your thread you have to call mRunning = false and then t.interrupt().

S.D.
  • 29,290
  • 3
  • 79
  • 130
  • when i use the thread reference myThread = new Thread(new Runnable... , i get this message showing "Type mismatch: cannot convert from void to Thread" – newbee Feb 22 '13 at 12:17
1

according to your current situation based on previous answer comment . you r trying like:

myThread = new Thread(new Runnable

right?

then u should also delete .start(); part of your previous coding. then write:

myThread.start();

u should declare myThread as global in your class, so that u can access this thread from anywhere in your class. now u can stop thread in onClick

u should combine this answer and User117 s answer together .

Shoshi
  • 2,254
  • 1
  • 29
  • 43
0

instead of
while(true)

inside the run() Method, declare a boolean variable to true and change it after onClick. For example:

     private boolean shouldRun = true; 

then in the run() method:

      @Override
    public void run() {
        // TODO Auto-generated method stub
        while (shouldRun==true) {
                     .
                     . 
                     .

and in your onClick:

        @override
          public void OnClick(View v){

              shouldRun=false;
                      .
                      .
                      .
Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • when i use the thread reference myThread = new Thread(new Runnable... , i get this message showing "Type mismatch: cannot convert from void to Thread" – newbee Feb 22 '13 at 12:17