【问题标题】:runOnUiThread without Activity [duplicate]没有活动的runOnUiThread [重复]
【发布时间】:2018-07-20 10:27:56
【问题描述】:

我在 android studio 中创建了一个 java 类,我想在这个类中使用 runOnUiThread()。 我可以在 Android 中运行没有 Activity.xml 的 runOnUiThread() 线程吗? 如果答案是肯定的?比怎么样?

【问题讨论】:

    标签: java android multithreading android-runonuithread


    【解决方案1】:

    是的。你只需要使用Handler

     new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
    
                    }
                });
    

    【讨论】:

    • 用这个代码sn-p我可以更新UI吗?
    • @nisarg 是的,你可以
    • 也可以不用调用new Runnable() { },也可以使用lambda:() -> { }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 2021-05-24
    • 2019-03-05
    • 2014-07-12
    相关资源
    最近更新 更多