gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:57 24 4
gpt4 key购买 nike

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

我有文件:

主要 Activity

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();
}
}
}
}

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

最佳答案

您可以实现自定义Interface,以便从您的主要 Activity 中处理它。在您的 SampleThread 上:

public interface TextViewChangeListener
{
public void onTextViewChanged(String newName);
}
TextViewChangeListener mListener;

然后,无论您希望在 TextView 中使用新名称,都可以调用 mListener.onTextViewChanged(String newName)。请记住首先使用 MainActivity 的实例初始化 mListener,否则您将收到空指针异常。您可以在 SampleThread 的构造函数中或通过为此目的创建一个方法来执行此操作。

在您的 Activity 中,您应该实现 SampleThread.TextViewChangeListener覆盖 onTextViewChanged

@Override
public void onTextViewChanged(String newName)
{
//MyTextView.setText(newName);
}

编辑:未经测试的代码:主要 Activity :

public class MainActivity extends ActionBarActivity implements SampleThread.TextViewChangeListener {
@Override
public void onTextViewChanged(Message msg)
{
// process incoming messages here
// i want to change the text of txt1 here
}

public static Handler mHandler;
Runnable thread = new SampleThread(this);
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);
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
{
public interface TextViewChangeListener
{
public void onTextViewChanged(Message msg);
}

public SampleThread(TextViewChangeListener mListener)
{
this.mListener = mListener;
}

TextViewChangeListener mListener;

@Override
public void run() {
int two = 0;
while(two<10) {
two++;
try {
Thread.sleep(1000);
mListener.onTextViewChanged(String.valueOf(two + "sec"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

请告诉我这是否有帮助。

关于java - Android主线程与其他线程之间的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28072858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com