Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
lwc:programming:mobile:android:threads:handlers [2022/02/23 14:11] – created John Harrison | lwc:programming:mobile:android:threads:handlers [2022/02/23 15:37] (current) – John Harrison | ||
---|---|---|---|
Line 46: | Line 46: | ||
} | } | ||
</ | </ | ||
+ | ==== Pass message then receive back from the other class ==== | ||
+ | <code Java> | ||
+ | // first we create the class and implement the callback interface | ||
+ | public class GetMessageClass implements Handler.Callback { | ||
+ | |||
+ | Handler handler; // get the mainUI handler for messaging back | ||
+ | |||
+ | public GetMessageClass(Handler h) { | ||
+ | handler = h; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public boolean handleMessage(@NonNull Message msg) { | ||
+ | // parse the message using the message key | ||
+ | Bundle bundle = msg.getData(); | ||
+ | String string = bundle.getString(" | ||
+ | Log.v(" | ||
+ | |||
+ | // boilerplate to send a message back | ||
+ | Message msg2 = handler.obtainMessage(); | ||
+ | Bundle bundle2 = new Bundle(); | ||
+ | bundle2.putString(" | ||
+ | msg2.setData(bundle2); | ||
+ | handler.sendMessage(msg2); | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | and in the main activity: | ||
+ | <code Java> | ||
+ | protected void onCreate(Bundle savedInstanceState) { | ||
+ | super.onCreate(savedInstanceState); | ||
+ | setContentView(R.layout.activity_main); | ||
+ | |||
+ | // after we have a class with a callback, we create a thread for the handler. | ||
+ | // every handler has a looper which actually calls the handler: | ||
+ | HandlerThread handlerThread = new HandlerThread(" | ||
+ | handlerThread.start(); | ||
+ | Looper looper = handlerThread.getLooper(); | ||
+ | |||
+ | // if you want to send a message back to the main activity in the main thread | ||
+ | // create a handler for it in the main thread and put the function inside the handler: | ||
+ | Handler mainThreadHandler = new Handler(getMainLooper()) { | ||
+ | @Override | ||
+ | public void handleMessage(Message msg) { | ||
+ | Bundle bundle = msg.getData(); | ||
+ | Log.v(" | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | // create an instance of the class and pass the main thread handler to it for messaging back | ||
+ | GetMessageClass getMessageClass = new GetMessageClass(mainThreadHandler); | ||
+ | // create a new handler and assign it to the looper in the handler thread | ||
+ | Handler handler = new Handler(looper, | ||
+ | |||
+ | // boilerplate to send a message | ||
+ | Message msg = handler.obtainMessage(); | ||
+ | Bundle bundle = new Bundle(); | ||
+ | bundle.putString(" | ||
+ | msg.setData(bundle); | ||
+ | handler.sendMessage(msg); | ||
+ | } | ||
+ | </ | ||
+ | | ||
+ | There are examples as yet unexplored of handlers posting runnables. For example, this sort of thing should work according to my brain and not tested by a living breathing compiler: | ||
+ | <code Java> | ||
+ | handler.post(new Runnable() { | ||
+ | @Override | ||
+ | public void run() { | ||
+ | Log.v(" | ||
+ | } | ||
+ | }); | ||
+ | </ | ||
+ | ===== Resources ===== | ||
+ | * https:// | ||
+ |