运行在回调为主线 [英] Run Callback On Main Thread

查看:164
本文介绍了运行在回调为主线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些code,它与Android的Facebook SDK进行交互,异步。不幸的是,这意味着当它返回它在后台线程。

I have some code that interacts with the Android Facebook SDK, Asynchronously. Unfortunately this means when it returns it is in a background thread.

科科斯-2DX prefers我在主线程与它进行互动,做事情特别是当像告诉主任切换场景(由于涉及的Open GL)

Cocos-2dx prefers me to interact with it in the Main Thread, especially when doing things like telling the Director to switch scenes (As it involves Open GL)

有没有办法得到一些code到主线程上运行?

Is there any way to get some code to run on the Main thread ?

推荐答案

只要你有一个背景下,你可以做这样的事情:

As long as you have a Context, you can do something like this:

Handler mainHandler = new Handler(context.getMainLooper());

和在UI线程上运行code:

And to run code on UI thread:

mainHandler.post(new Runnable() {

    @Override
    public void run() {
        // run code
    }
});

的建议通过卡卡

您也可以使用静态<一个href=\"http://developer.android.com/reference/android/os/Looper.html#getMainLooper%28%29\">Looper.getMainLooper()这

返回应用程序的主尺蠖,它生活在应用程序的主线程。

Returns the application's main looper, which lives in the main thread of the application.

这篇关于运行在回调为主线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆