java - Asynctask and Handler class - can't share variable -
i have service includes asynctask , handler classes. asynctask , handler should share variable. use static volatile variable issendmessage
because think there 2 threads, i'm not sure. problem variable in while loop doesn't change. don't know mistake is. here snippet of code.
public class services extends service { static volatile boolean issendmessage = false; @override public ibinder onbind(intent intent) { return null; } @override public int onstartcommand (intent intent, int flags, int startid) { new connectionserverasynctask().execute(); return service.start_sticky; } public class connectionserverasynctask extends asynctask<void, result, void> { private boolean finish = false; @override protected void onpreexecute() { super.onpreexecute(); } @override protected void doinbackground(void... params) { while(!finish){ log.i("issendmessage",string.valueof(issendmessage)); } } @override protected void onpostexecute(void avoid) { super.onpostexecute(avoid); } } public static class messagehandler extends handler{ @override public void handlemessage(message msg) { log.i("messagehandler","handlemessage"); issendmessage = true; } } }
onpostexecute() automatically uses ui thread there's not need use handler in combination async task. write code want executed there anywhere else.
also loop while(!finish), didn't see anyplace finish set true.
Comments
Post a Comment