Fragment中的线程只调用一次

问题描述 投票:-1回答:2

我正在尝试实现一个在Fragment中更改UI上的内容的线程。因此我需要参考主线程。根据我的研究,我发现以下代码应该可以解决问题:

new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(menuActivity, "HELLO", Toast.LENGTH_SHORT).show();
        }
    });

这将只执行一次,即使Looper通常应保持线程活着。试图在Looper.prepare()内调用Handler将导致RuntimeException,因为每个线程只允许一个Looper。编辑:我的目标是每秒永久更新TextView。

我也尝试过以下方法:

Thread t = new Thread() {
        @Override
        public void run() {
            menuActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("-----------TEST");
                }

            }); 
        }
    };
t.start();

但这也只会执行一次。

我也读过this article,但我想我的第一段代码只是文章中所示代码的较短版本。

我的错误可能出现在这些代码片段中?

这个问题并不重复,因为我提出了一个完全不同的代码片段,这是我遇到的问题的基础。此外,在这个主题中更深入地解释了Looper。

android multithreading ui-thread
2个回答
1
投票

我知道你想在1秒后重复更新文本视图。这是我刚才写的简单演示。

样机屏幕

enter image description here

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_view_money"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:layout_marginTop="30dp"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="startUpdateTextViewMoney"
            android:text="Start" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="stopUpdateTextViewMoney"
            android:text="Stop" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/edit_text_money"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="setMoney"
            android:text="SET MONEY" />

    </LinearLayout>

</LinearLayout>

main activity.Java

public class MainActivity extends AppCompatActivity {
    private static final int UPDATE_TEXT_MONEY_INTERVAL = 1000;

    private Handler mMainHandler;

    private TextView mTextViewMoney;
    private TextView mEditTextMoney;

    private String money;

    private Runnable mUpdateTextViewMoneyTask = new Runnable() {
        @Override
        public void run() {
            if (TextUtils.isEmpty(money)) {
                mTextViewMoney.setText(String.valueOf(SystemClock.elapsedRealtime()));
            } else {
                mTextViewMoney.setText(money);
                money = null;
            }
            mMainHandler.postDelayed(this, UPDATE_TEXT_MONEY_INTERVAL);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextViewMoney = findViewById(R.id.text_view_money);
        mEditTextMoney = findViewById(R.id.edit_text_money);

        mMainHandler = new Handler(Looper.getMainLooper());
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopUpdateTextViewMoney(null);
    }

    public void startUpdateTextViewMoney(View view) {
        mMainHandler.post(mUpdateTextViewMoneyTask);
    }

    public void stopUpdateTextViewMoney(View view) {
        mMainHandler.removeCallbacks(mUpdateTextViewMoneyTask);
    }

    public void setMoney(View view) {
        String money = mEditTextMoney.getText().toString();
        this.money = !TextUtils.isEmpty(money) ? money : "";
    }
}
  • 当用户按下“开始”按钮时,应用程序将每秒开始更新文本视图
  • 当用户按“停止”按钮时,应用程序将停止更新文本视图。
  • 如果用户想要设置下一次要显示的新资金,只需输入编辑文本,然后按设置资金。

0
投票

这将只执行一次,即使Looper通常应保持线程活着。

您似乎对Looper的目的/功能感到困惑。 Looper保持线程活着。如果主线程未保持活动状态,则应用程序将退出。然而,Looper不会重复执行张贴到与之相关的Runnable / HandlerThreads。每个Runnable只执行一次。如果要多次执行相同的Runnable,则必须多次发布。例如:

Handler mainThreadHandler = new Handler(Looper.getMainLooper());
Runnable doToast = new Runnable() {
        @Override
        public void run() {
            Toast.makeText(menuActivity, "HELLO", Toast.LENGTH_SHORT).show();
        }
    });
mainThreadHandler.post(doToast); // Queue the first execution of the code in the Runnable's run() method. 
mainThreadHandler.post(doToast); // Queue the second execution of the code in the Runnable's run() method.
© www.soinside.com 2019 - 2024. All rights reserved.