2

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

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

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

My question is, how do I pass the variable sbprint into my new screen in another class using handler just like above.

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

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.

8
  • 1
    I'm not sure it works for you but you can store your data in shared preferences and retrieve it anywhere in your application. Don't use serialisable since it makes your app too slow.
    – Hesam
    Dec 9, 2013 at 5:22
  • yes I'm aware of that. Any recommended link about shared preferences? Dec 9, 2013 at 5:27
  • Best reference is Android documentations. Please refer to following link: developer.android.com/training/basics/data-storage/…
    – Hesam
    Dec 9, 2013 at 5:35
  • so is this shared preferences compatible for handling the updated variable over time? which is more efficient between shared preference and static variable? Dec 9, 2013 at 5:41
  • 1
    I guess your problem is something else. Based on what you said in last paragraph, "...my asynctask that works inside my MainActivity stopped as the screen change to another class". If you mean you have two activities, from fist activity you are running AsyncTask then by calling Second activity, first activity goes to onStop() and even onDestroy(). That's why your AsyncTask stops. Instead of AsyncTask use Android Services and run it from your first activity. you can search about it. Then it's alive as long as your application is alive.
    – Hesam
    Dec 9, 2013 at 18:42

1 Answer 1

1

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.

2
  • i'm sorry i just heard the interface thing. Is this different way of passing data just like intent? why would i want to use interface? thanks Dec 9, 2013 at 5:43
  • one of the uses for an interface is to create a callback (i.e. a listener like onClickListener()) which is what you might want in this case. Look at the link for a more detailed explanation docs.oracle.com/javase/tutorial/java/concepts/interface.html
    – Emmanuel
    Dec 9, 2013 at 5:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.