How to pass data from separate thread class to activity in Android How to pass data from separate thread class to activity in Android multithreading multithreading

How to pass data from separate thread class to activity in Android


Pass your mHandler as parameter to the constructor of you RecordThread class, and use mHandler.obtainMessage( ... ).sendToTarget() to pass data to the Activity class

On the RecordActivity class, declare and use the Handler as:

private final Handler mHandler = new Handler() {    @Override    public void handleMessage(Message msg) {    }

Then it dependes on how you called the obtainMessage(), if you used, for example, obtainMessage(int what, int arg1, int arg2) you can access these by using msg.what, msg.arg1, and msg.arg2.


I would recommend taking a look at the Android fundamentals. It gives you a good overview of the key application components within Android. I think this is a must read for anyone starting with Android.

A service might be what you need.

Keep in mind that services run on the main thread, so when executing expensive operations, you may want to checkout Handling Expensive Operations in the UI Thread.