Created
September 17, 2014 02:58
-
-
Save garymabin/0268abaca2ad18ecf21c to your computer and use it in GitHub Desktop.
Code for exchanging Parcelable data between work thread and main thread
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface INotifier { | |
void onNewData(Parcelable payload); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WorkThread { | |
private INotifier mNotifier; | |
public void run() { | |
Parcelable payload; | |
//do your network relate stuff | |
mNotifier.onNewData(payload); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WorkTracker implements INotifier { | |
private Handler mHandler = new Handler() { | |
public void handleMessage(Message msg) { | |
//run on ui thread now! | |
Parcelable payload = (Parcelable)msg.obj; | |
} | |
}; | |
public void onNewData(Parcelable payload) { | |
//called on WorkThread, use Handler to change to main thred. | |
mHandler.sendMessage(Message.obtain(null,0,0,payload)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment