Android如何在其他类中运行OnUiThread? [英] Android how to runOnUiThread in other class?

查看:28
本文介绍了Android如何在其他类中运行OnUiThread?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,在 MainActivity 中,有一个工作正常的线程.但是当我调用另一个类从服务器获取数据时,我无法在线程上运行.请参阅下面的代码示例.

In my application, in MainActivity, there is a thread which works fine. But when I call another class to get data from the server I can't run on a thread. See code example below.

class MainActivity extends Activity implements Runnable {

    public void onCreate() {
        new Thread(this).start();
    }

    public void run() {
        //here is code for download data from server after completion this and in handler  i m call other class in setdata() method....
    }

    public void setData() {
        new CheckData(this);
    }
}

class CheckData {
    public CheckData(Context context) {
        context.runUIonthread(){//cant call as runUIthread............
    }
}

推荐答案

如果您不想传递上下文,这里有一个解决方案:

Here's a solution if you don't want to pass the context:

new Handler(Looper.getMainLooper()).post(new Runnable() {
    public void run() {
        // code goes here
    }
});

这篇关于Android如何在其他类中运行OnUiThread?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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