How do I post code to be run on the Android main thread from a separate thread in C++?

后端 未结 5 1382
我寻月下人不归
我寻月下人不归 2021-01-07 17:55

I have a separate thread running in C++ in the background and I want it to be able to post code to be run on another thread that\'s already running an android.os.Looper (e.g

5条回答
  •  时光取名叫无心
    2021-01-07 18:04

    If you want to make some stuffs in the main thread from another thread, I suggest you to use the runOnUiThread function. The main thread in Android is the User Interface thread. I'm not sure if you can use this function in the ndk code.

    An example of code could be this:

    private void runOnMainThread() {

    runOnUiThread(new Runnable(){       
       public void run() {            
       try {
           // do some stuffs
       } catch (final Exception ex) {
           // handle the possible exception           
       }      
    }         });   }
    

    Anyway I suggest you to read the follow links: link1, link2, link3.

    I hope it helps.

提交回复
热议问题