Android 后台活动始终运行时如何在两个活动之间传递数据

Android 后台活动始终运行时如何在两个活动之间传递数据,android,sharedpreferences,handler,background-process,Android,Sharedpreferences,Handler,Background Process,如下图所示。每当我将屏幕更改为B类时,我希望A类中的后台活动(蓝牙数据流)始终运行。UI仅更新屏幕活动的位置,但使用始终运行的相同后台活动 我知道Android上只允许运行一个活动。这就是我尝试使用共享首选项从类A上的textview UI update传递数据的方式,并尝试在类B中获取数据。但是,当我将屏幕更改为类B时,后台活动停止工作。因此,我之前设置的共享首选项只传递了最后的数据 代码如下: 获取数据,A类 h = new Handler() { public v

如下图所示。每当我将屏幕更改为B类时,我希望A类中的后台活动(蓝牙数据流)始终运行。UI仅更新屏幕活动的位置,但使用始终运行的相同后台活动

我知道Android上只允许运行一个活动。这就是我尝试使用共享首选项从类A上的textview UI update传递数据的方式,并尝试在类B中获取数据。但是,当我将屏幕更改为类B时,后台活动停止工作。因此,我之前设置的共享首选项只传递了最后的数据

代码如下: 获取数据,A类

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;
                }
            };
        };
接收B类数据

package com.oding.skripsibluetooth3;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;


public class Datalog extends Activity{

    TextView tvDatalog;

    @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");
    }



}
我很欣赏任何想法, 我发现有人已经遇到了。我试着在变量sbprint上加上static,就像前面提到的解决方案一样,红色警告说“非法修改变量sbprint;只允许final” 对于我遇到的具体问题,哪种方法更简单?共享首选项?静态变量?还是什么?我该如何解决这个问题?
谢谢

您必须创建
服务
作为后台工作人员。然后,您可以在
B
活动中绑定到此服务,并获取计算值。看一看就知道了

您已经问过这个问题请看最后一条评论,这是一个与以前不同的问题。
服务
在某种程度上类似于
异步任务
?异步任务是一个后台线程,通常是一次性执行。服务也是后台线程,通常是长时间运行的进程。