Threads in Android Part 1 (infinite loop)

This series shows tips and tricks about using threads in android development. Part 1 describes how to create a thread with an infinite loop. Since we only need the thread while the app is running we must assure that the thread stops when the app is not active.

In this example we create a thread which will execute every n seconds. Look a the following code. We override the onCreate method of the activity and create a thread which will do it’s work and sleep afterward.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 
    thread = new Thread() {
        public void run() {
            while (true) {
                try {
                    // do something here
                    Log.d(TAG, "local Thread sleeping");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Log.e(TAG, "local Thread error", e);
                }
            }
        }
    };
    thread.start();
}

What happens if the app is pausing? For instance another app comes to the foreground. The following screen shot from the debugger shows that the thread is still running while the app is not active: Debugger: thread is still running

So we need to control the thread and stop it in the onPause method.

The solution is taken from the google article Updating the UI from a Timer. We instantiate a Handler which allows us to process Runnable objects.

The Handler it is bound to the main UI thread of the activity. By calling handler.postDelayed(this, 1000); the new thread is added to message queue (of the main UI thread) and will be run after 1 second.

private Thread thread;
private Handler handler = new Handler();
 
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
 
	thread = new Thread() {
		public void run() {
			// do something here
			Log.d(TAG, "local Thread sleeping");
			handler.postDelayed(this, 1000);
		}
	};
}

The rest is easy. We start the thread in the onResume, which is called after onCreate and when the activity comes to the foreground, by calling handler.postDelayed(thread, 0); (parameter 0 shows that there is no delay).

Before adding the thread to the handler we may remove it, to make sure it is not already bound to the handler. onPause does the same:

@Override
protected void onResume() {
	super.onResume();
 
	handler.removeCallbacks(thread);
	handler.postDelayed(thread, 0);
	Log.d(TAG, "onResume");
}
 
@Override
protected void onPause() {
	super.onPause();
 
	handler.removeCallbacks(thread);
	Log.d(TAG, "onPause");
}

Looking at the debugger we notice that the thread stopped after pausing the activity: Threads in Android - onResume / onPause

ipcas FloppyEmulator V3: completely jumper-less configuration / parameter settings without jumpers – Video

The ipcas FloppyEmulator V3 supports nearly all disk formats. Also special Formats like CP/M, DEC Rainbow, extended DOS or Stäubli JC3/4/5 are supported.
The parameterization and configuration is complete without jumper.

More information about the FloppyEmulator V3 >>

New Configtool Version 15.1 for 32-bit and 64-bit Windows systems


The configuration tool for products of the ipEther232 family has been thoroughly revised and is now available for download on the respective product websites and in the support section.

Certified Windows drivers enable the installation under

  • Windows XP with ServicePack 3 (32-Bit)
  • Windows Server 2003/Vista/7/Server2008 (32-Bit and 64-Bit)
  • Windows Server 2008 R2 (64-Bit)

Apart from some corrections, the main focus was to improve and enhance the 64-bit driver for ipEther232.

The configuration tool is available for the following products:

ipEther232Virtual COM port
ipEther232.ModemEthernet Modem
ipEther232.PPPPPP – Gateway
ipEther232.IOI/O Data logger / Event logger
ipNTPNTP time server

Data Respons Medical Solutions at Embedded World 2011

Our parent company, Data Respons is presenting medical IT solutions, embedded IT products and implemented project solutions at Stand 229, Hall 9.

Besides Medical Panel PC’s and Displays you will also find the fanless Medical Box PC and the Panasonic Toughbook H1 among the special highlights.

Medical Toughbook
The Toughbook H1 ensures that caregivers have all the tools they need in a single mobile clinical assistant (MCA). This medical touchscreen device has hot-swappable twin batteries and is IP65 certifed. In addition to a camera on the back, this compact device has RFID and a barcode scanner on the bottom.

Medical Box-PC
The Medical Box-PC is a Intel® Core™ i7 Fanless System with FireWire (IEEE 1394b), eSATA, HDMI, VGA, DVI, USB, 2x GbE LAN, RS232/422/485 and more interfaces.
If that’s not enough, the Medical PC can expand via PCI respectively PCIe.

Visit the Data Respons at stand 229 in hall 9, it’s worth it.
Today is the last day of embedded world 2011.

ipcas hardware solutions at Embedded World


Today and tomorrow, we would be happy to see you and give you a live display of our produce. Visit us at embedded world 2011, Stand 129 in Hall 9 in the Nuremberg Trade Fair Center (Nürnberg Messe).

Summary of our product range:

Embedded Systems / Industrial PC LB3

Ethernet and USB I/O Extension

USB-Input-Device with temperature sensor

Floppy Disk Drive (FDD) Emulators

embedded world 2011: minimal video tour on ipcas stand 129 in hall 9

Visit us today and tomorrow, Nuremberg Trade Fair Center (Messezentrum Nürnberg)

Visit us at embedded world 2011


We will be more than glad to support you with first-hand information on our highly innovative Hardware and Software products and concepts. We would be happy to see you and give you a live display of our produce from today until 3 March 2011 at Stand 129 in Hall 9 in the Nuremberg Trade Fair Center (Messezentrum Nürnberg).

Also this year we proudly present a very special highlight:

Floppy emulator V3

The extended version of our Floppy-Emulator is to be at the ready.
In addition to higher compatibility, even to exotic disk formats, the configuration and setup is all done via graphical user interface on the PC.
Now jumpers are a thing of the past.

Take the chance to see the Floppy-Emulator 2 live at the embedded world 2011 until Thursday. Visit us at Hall 9, Stand 129.