Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6845317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:29:10+00:00 2026-05-27T00:29:10+00:00

I just begun with a simple thread handler program on Android. The code works.

  • 0

I just begun with a simple thread handler program on Android. The code works. The only part is on how to shut down the handler. I am trying in vain to close the handler onPause() but it simply keeps running. Any suggestions?

Here is the code:
My Main Activity thread:

package com.test.prog;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class AndroidThreadProgv1Activity extends Activity {

    private Handler DataHandler;

    private DataUpdater du;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);       
        setContentView(R.layout.main);

        /* Initialize handler to allow UI updating in other threads */
        DataHandler = new Handler();

        startDataUpdater();

    }

    public void onPause(){
        super.onPause();
        du.stopConnection();
    }

    private void startDataUpdater(){
        System.out.println("In startDataUpdater");
        du = new DataUpdater(DataHandler,(TextView) findViewById(R.id.textView1));
        du.start();
    }

}

DataUpdater.java:

package com.test.prog;

import java.io.IOException;
import android.os.Handler;
import android.os.Looper;
import android.widget.TextView;

/*
 * Data updater thread.  Updates the display with the response.
 */
public class DataUpdater extends Thread{

    /*Handler for the UI update*/
    private Handler dataRefreshHandler;

    /*The UI text displaying the data*/
    private TextView dataLabel;

    /*data value*/
    private int data = 0;
    /*data update rate in ms*/
    private static final int dataUpdateRate = 2000;

    private boolean stoppedFlag= false;

    public DataUpdater(Handler handler, TextView tv){
        this.dataRefreshHandler = handler;
        this.dataLabel = tv;
    }


    public void stopConnection()
    {
        if(this.dataRefreshHandler != null) {
            this.dataRefreshHandler.removeMessages(data); // Remove a delayed message  
            System.out.println("In stopConnection"); 
            /*
           if(!stoppedFlag){
                this.dataRefreshHandler.getLooper().quit(); // Warning
                stoppedFlag = true;
            }
            */
        }
    }


    @Override
    public void run(){
        try {
            while(true){
                System.out.println("In run");
                data = data + 1;        
                updateData();               
                Thread.sleep(dataUpdateRate);
            }

        } 
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }       
    /*
     * Calls the UI Handler and updates the display
     */
    private void updateData(){
        dataRefreshHandler.post(new Runnable() {
            public void run() {
                if(data==0)
                {
                    dataLabel.setText("Value not known");
                }
                else
                {
                    System.out.println("In update");
                    dataLabel.setText("Value="+data);

                }
            }
        });
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:29:11+00:00Added an answer on May 27, 2026 at 12:29 am

    Your “run” method in dataUpdater has a while(true) loop. You never check for any exit condition so it will never stop. What you could do is include a volatile flag in DataUpdater (initially set to true) and have stopConnection set it to false. Your while loop in run can check that flag instead of just looping infinitely.

    To make sure your DataUpdater is running whenever your activity is running:

    Move the call to startDataUpdater(); from onCreate to onResume (Android always calls onResume after onCreate so this will ensure that you are always calling startDataUpdater on creation as well as on resuming after pause).

    Change your run method in DataUpdater to look like this:

    public void run(){
       isRunning = true; // isRunning should be declared as a "private volatile boolean"
        try {
            while(isRunning){
                System.out.println("In run");
                data = data + 1;        
                updateData();               
                Thread.sleep(dataUpdateRate);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just begun trying out WatiN as part of my testing platform for web
I have just begun working with the Android SDK and I am having problems
I just began learning network programming. My first program is very simple: once connected,
I've just finished reading 'Java for Dummies' and have begun to create a simple
Just began learning about algorithms in c++. My book mentions a simple read-only algorithm
Greetings, fellow StackOverFlow members. I have just begun to learn how to program web-enabled
I just started c++ today. I am doing some simple registration program. I want
I'm practising with C, writing simple programs. The little program below should just get
I've just begun using LINQPad and so far I like it but most tutorials
I have just begun working on a project which uses Mercurial as a version

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.