Java 主线程与其他线程之间的Android通信

Java 主线程与其他线程之间的Android通信,java,android,Java,Android,我想动态更改textview的文本,但是如果我想创建一个游戏线程,我需要相同的逻辑,所以我需要在主线程和第二线程之间进行通信 我有以下文件: 主要活动 public class MainActivity extends ActionBarActivity { public static Handler mHandler; Runnable thread = new SampleThread(); TextView txt1 = (TextView) findViewBy

我想动态更改textview的文本,但是如果我想创建一个游戏线程,我需要相同的逻辑,所以我需要在主线程和第二线程之间进行通信

我有以下文件:

主要活动

public class MainActivity extends ActionBarActivity {

    public static Handler mHandler;
    Runnable thread = new SampleThread();
    TextView txt1 = (TextView) findViewById(R.id.txt1);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        //hiding status bar
        if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
        setContentView(R.layout.activity_main);


        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                // process incoming messages here
                // i want to change the text of txt1 here
            }
        };
        new Thread(thread).start();


    }
}
样品线

package com.example.katsar0v.myapplication;

import android.util.Log;

/**
 * Created by Katsar0v on 1/21/2015.
 */
public class SampleThread implements Runnable {

    @Override
    public void run() {
        int two = 0;
        while(two<10) {
            two++;
            try {
                Thread.sleep(1000);

                //instead of logging, i want to send the text to main UI
                Log.d("MSG", String.valueOf(two + "sec"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

我看到的问题是,当我的线程位于另一个文件中时,如何使用处理程序更改文本?或者我应该在第一个类中使第二个类成为静态的,当代码变得很长时,我应该怎么做?它不能全部放在一个文件中?

您可以实现一个自定义接口,以便从主活动中处理它。 在样本线程上:

公共接口TextViewChangeListener { public void onTextViewChangedString newName; } TextViewChangeListenerMListener; 然后调用mListener.onTextViewChangedString newName,只要您想在TextView中使用新名称。请记住,首先使用MainActivity的实例初始化mListener,否则将出现空指针异常。您可以在SampleThread的构造函数中或为此创建一个方法来实现这一点

在活动中,您应该实现SampleThread.TextViewChangeListener并重写onTextViewChanged

@凌驾 public void onTextViewChangedString newName { //MyTextView.setTextnewName; } 编辑:未测试代码: 主要活动:

公共类MainActivity扩展ActionBarActivity实现SampleThread.TextViewChangeListener{ @凌驾 public void onTextViewChangedMessage msg { //在此处处理传入消息 //我想在这里更改txt1的文本 } 公共静态处理器; Runnable thread=新的SampleThreadthis; TextView txt1=TextView findViewByIdR.id.txt1; @凌驾 受保护的void onCreateBundle savedInstanceState{ super.onCreatesavedInstanceState; ActionBar ActionBar=getSupportActionBar; actionBar.hide; //隐藏状态栏 如果Build.VERSION.SDK_INT<16{ getWindow.setFlagsWindowManager.LayoutParams.FLAG_全屏显示, WindowManager.LayoutParams.FLAG_全屏显示; }否则{ View decorView=getWindow.getDecorView; int uiOptions=View.SYSTEM\u UI\u FLAG\u全屏显示; decorView.SETS系统可分离性; } setContentViewR.layout.activity_main; 新Threadthread.start; } } 样本线程:

包com.example.katsar0v.myapplication; 导入android.util.Log; /** *由Katsar0v于2015年1月21日创建。 */ 公共类SampleThread实现可运行 { 公共接口TextViewChangeListener { public void onTextViewChangedMessage msg; } 公共示例ThreadTextViewChangeListener MLListener { this.mListener=mListener; } TextViewChangeListenerMListener; @凌驾 公开募捐{ int 2=0;
而您可以在中找到一些示例,它对UI线程做了大量工作。例如,有一对处理程序,一个用于UI线程,一个用于渲染器线程。在onResume中,您可以看到主线程通过构造函数将其处理程序传递给渲染器,然后通过方法调用检索渲染器线程的处理程序

具有稍微不同的方法,使用同时实现回调接口的处理程序。处理程序对象作为接口实例传递给CircularCoder构造函数。公共回调方法在内部使用该处理程序

唯一棘手的一点是,如果要将处理程序从非UI线程中传递出去,则需要在线程启动之前执行此操作,或者使用适当的线程同步操作来避免

你不需要让你的类在同一个文件中,除非一个嵌套在另一个文件中,否则你真的不应该。如果它们在同一个包中,那么默认的包作用域将允许它们彼此看到。Grafika的第一个示例使用嵌套/私有类,第二个示例更分散

当然,如果您所要做的只是从非UI线程提交UI事件,那么您可以使用