Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Notify Thread Information Activity In Android App Development

DZone's Guide to

Notify Thread Information Activity In Android App Development

Creating multi-thread applications for Android application development is a challenging task for many Android developers.

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

Creating multi-thread applications for Android application development is a challenging task for many Android developers. Single and multi-threading approaches are used to create complex Android enterprise mobile apps, as they help to streamline functional operation of the code. But sometimes it is  necessary to update the UI from the background thread about the operations performed. If you ever tried to access an UI element from a background thread, you have already noticed that an exception is thrown. This article will explain  how to notify activity with the information posted by another thread.

For creating multi-thread apps, Android by default does not allow the developer to modify the UI outside of the main thread. This problem is faced by many coders and if you still managed to do it you’d be breaking the second rule of the single-threaded model which is “do not access the Android UI toolkit from outside the UI thread“, as stated here http://developer.android.com/guide/components/processes-and-threads.html.

Problem

While creating complex multi-thread functions in an enterprise android app, the information generated from these threads are not notified in the UI resulting in mismatch of app business logic and crashing of the application.

Solution

  • Implement a Handler class, override method handleMessage() which will read messages from thread queue

  • Next post message using sendMessage() method in worker thread

There are many situations when it is required to have a thread running in the background and send information to main Activity's UI thread. From the architectural level we can use two different approaches for notifying thread activity.

  1. Use of Android AsyncTask class

  2. Start a new thread

    Using AsyncTask is very convenient as there might be situations when you really need to construct a worker thread by yourself. In such situation, you  will need to send some information back to Activity thread. Keep in mind that Android doesn't allow other threads to modify any content of main UI thread as stated above. Instead you're required to wrap data into messages and send them through message queue.
    You can implement this operation in two parts:

Part 1 – Add Handler

Add an instance of Handler class to your MapActivity instance.

public class MyMap extends MapActivity {
. . . .
public Handler _handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.d(TAG, String.format("Handler.handleMessage(): msg=%s", msg));
// This is where main activity thread receives messages
// Put here your handling of incoming messages posted by other threads
super.handleMessage(msg);
}
};
. . . .
}

Part 2 – Post Message

In the worker thread post a message to activity main queue whenever you need Add handler class instance to your MapActivity instance.

/**
* Performs background job
*/
class MyThreadRunner implements Runnable {
// @Override
public void run() {
while (!Thread.currentThread().isInterrupted()) { 
// Just dummy message -- real implementation will put some meaningful data in it
Message msg = Message.obtain();
msg.what = 999;
MyMap.this._handler.sendMessage(msg);
// Dummy code to simulate delay while working with remote server
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}

Try out the above code, and if you still are not able to notify activity information then feel free to get in touch.

Do check our blog explaining Mobile Application Testing: Best Practices . And have a look at our Android Mobile Application Development and Testing Services.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:
mobile applications

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}