How pass data between two activities while background activity always running

So as illustration below. i want my background activity (stream data from bluetooth) places in class A always running whenever i change my screen to class B. The UI only update where the screen active, but using the same background activity that always running.

I'm aware that there's only one active activity allowed to run on Android. That's way i try to pass the data using shared preferences from textview UI update on class A, and try to get it in class B. But, when i change the screen to class B, the background activity stop working. So, my shared preferences that i set up earlier only passed the last data.

here's the code : Obtain data, class 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);

                    //Log.d(TAG, "...String:"+ sb.toString() +  "Byte:" + msg.arg1 + "...");

receive data class B

package com.oding.skripsibluetooth3;

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

public class Datalog extends Activity{

    TextView tvDatalog;

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        tvDatalog = (TextView) findViewById(;

        SharedPreferences logPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        String text = logPreferences.getString("log", "null");


I appreciate any ideas, i found that there's someone already encounter the similar situation. And i tried to put static on variable sbprint just like the solution stated, and there's red warning say "Illegal modifier for the variable sbprint; only final is permitted" which one is simpler way for specific problem i have?shared-preferences?static variable?or what? and how do i resolve this? thank you


You have to create Service as background worker. Than you can bind to this service in your B Activity and get calculated values. Take a look here and here.

Need Your Help

Installed Oracle Express - Can't login

oracle login passwords username oracle-xe

I just installed Oracle Express edition. During the installation it asked me to set a password but not a username. After the installation completed, it directed me to a browser window and asked me to

A good source for WINAPI messages, message hierarchies, initialization routines

winapi messaging

Does anyone know where can I find a good resource on WINAPI messages?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.