[英]How to pass data[] from thread to Activity
I want to send continuously generated data[] from CalcThread which was created in MainActivity to GraphActivitity which was created in MainActivity too. 我想将连续生成的数据[] 从在MainActivity中创建的CalcThread发送到也在MainActivity中创建的GraphActivitity。
I'd like to use Handler technology to send multiply messages to Activity object. 我想使用处理程序技术将乘法消息发送到Activity对象。 Hope it is possible. 希望有可能。 I've checked whole bunch of proposals and found. 我检查了所有提案,然后找到了。 They are: single transmission or not using Handler or adding third-part components about. 它们是:单次传输或不使用Handler或不添加第三方组件。 As I understand using Messages and Handlers is convenient, safe and easy technology. 据我了解,使用消息和处理程序是一种方便,安全且容易的技术。 But it seems to me already not too easy, alas. 但是在我看来,这已经不太容易了,las。
In MainActivity:
//Start calculation thread
public void readStart(View v) {
audioRecorder = new AudioReceiver(myAudioFormat);
tAudioReceiverThread = new Thread(audioRecorder);
tAudioReceiverThread.start();
}
//Start Graph Activity
public void openGraph(View v) {
Intent intent = new Intent(MainActivity.this, GraphActivity.class);
Handler hh = new Handler();
audioRecorder.addHandler(hh);
intent.putExtra("fff", (Parcelable) hh);//my fantasy
startActivity(intent);
}
In AudioReceiver class:
public class AudioReceiver implements Runnable {
private List<Handler> handlers;
private AudioRecord mRecord;
public void addHandler(Handler handler)
{
handlers.add(handler);
}
private void sendMsg(short[] data)
{ for(Handler handler : handlers)
{
handler.sendMessage(handler.obtainMessage(1, data));
}}
In GraphActivity:
I have to get handler and register listener. Not sure.
My question is. 我的问题是。 How to register handler in list and transmit it to GraphActivity? 如何在列表中注册处理程序并将其传输到GraphActivity? I think i don't understand technology. 我想我不懂技术。 So may somebody explain what i skipped in school, please. 所以请有人解释一下我在学校跳过的内容。 Thank you. 谢谢。
OpenGraph function have to be like this. OpenGraph函数必须是这样的。 public void openGraph(View v) { Intent intent = new Intent(MainActivity.this, GraphActivity.class); Handler hh = new Handler(); audioRecorder.addHandler(hh); Bundle bundle = new Bundle(); bundle.putParcelable("hh",(Parcelable)hh); startActivity(intent,bundle); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.