multithreading - Android UI thread update from web service -
if want update ui background thread when message received web service call, below considered safe option? i'm worried potential memory leaks having handler in application class
public class myapplication extends application { private static long uithreadid; private static handler uihandler; @override public void oncreate() { super.oncreate(); uithreadid = thread.currentthread().getid(); uihandler = new handler(); } public static void customrunonuithread(runnable action) { if (thread.currentthread().getid() != uithreadid) { uihandler.post(action); } else { action.run(); } } }
and in class deals messages received in separate threads:
new thread( new runnable() { @override public void run() { // make web service call myapplication.customrunonuithread(new runnable() { @override public void run() { httpresponsehandler.onsuccess(); } }); } } }).start();
my alternative use parallel asynctasks doing work in doinbackground method , updating ui in onpostexecute method. both of these options work, i'm not sure 1 'most' correct speak.
better way register broadcastreceiver
, using localbroadcastmanager
send broadcast on receiving message
Comments
Post a Comment