如何从后台进程将数据传递到另一个活动 [英] How to pass the data from background process to another activity
问题描述
我能得到的数据显示,从主要活动保存在变量sbprint在屏幕上。该varible sbprint我从缓冲蓝牙数据背景活动获得。我想登录屏幕上的数据出现另一个类。
问题是我不能使用序列化,因为数据总是在实时模式下我的蓝牙模块流。基于这里 的code
I could get the data appear saved in variable sbprint on screen from main activity. The varible sbprint i get from background activity that buffer bluetooth data. I want to log the data appear on screen to another class. The problem is i can't use serializable since the data always stream from my bluetooth module in real-time mode. The code based on here
我用处理器从缓冲的数据更新我的UI
注意:我得到的内部处理程序共享preference(这里面MainActivity.java)
I use handler to update my UI from data buffered NOTE: I obtain shared preference inside handler (this is inside MainActivity.java)
h = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case RECIEVE_MESSAGE: // if receive massage
byte[] readBuf = (byte[]) msg.obj;
String strIncom = new String(readBuf, 0, msg.arg1); // create string from bytes array
sb.append(strIncom); // append string
int endOfLineIndex = sb.indexOf("\r\n"); // determine the end-of-line
if (endOfLineIndex > 0) { // if end-of-line,
String sbprint = sb.substring(0, endOfLineIndex); // extract string
sb.delete(0, sb.length()); // and clear
txtArduino.setText("Data from Arduino: " + sbprint); // update TextView
SharedPreferences logPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences.Editor editor = logPreferences.edit();
String textLog = txtArduino.getText().toString();
editor.putString("log", textLog);
editor.commit();
}
//Log.d(TAG, "...String:"+ sb.toString() + "Byte:" + msg.arg1 + "...");
break;
}
};
};
轰处理程序得到ConnectedThread类的消息对象根据此命令,
the h Handler get the message object from ConnectedThread class based on bluetooth documentation in this command,
h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget(); // Send to message queue Handler
我的问题是,我该如何使用处理器就像上面传递变量sbprint到我新的屏幕在另一个类。
My question is, how do I pass the variable sbprint into my new screen in another class using handler just like above.
以下是我收到了来自另一种传递数据。我从共享preferences得到传递变量缓冲区中的数据的建议
here's how i received data from data passed in another. I pass the data in variable buffer by getting from shared preferences as suggested
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datalog);
tvDatalog = (TextView) findViewById(R.id.tvDatalog);
SharedPreferences logPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String text = logPreferences.getString("log", "null");
tvDatalog.setText(text+"\r\n");
}
请注意:
我从我的MainActivity数据,但好像我的AsyncTask的作品里面我MainActivity停止,画面切换到另一个类。如何让我的背景活动也适用于这两个类。我的意思是,每当我改变我的屏幕上一个到另一个背景活动总是在工作。
NOTE: I got the data from my MainActivity but seems like my asynctask that works inside my MainActivity stopped as the screen change to another class. How do i make my background activity also works on both class. I mean whenever i am changing my screen on one to another the background activity always working.
推荐答案
如果我理解正确的话,你可能需要使用的接口。您可以定义包含处理程序
的活动
的接口。然后你就可以实现所需的类接口
。
If I understand correctly, you might want to use an interface. You can define an interface in the Activity
that contains the Handler
. Then you can implement the interface in the desired Class
.
这篇关于如何从后台进程将数据传递到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!