How to pass the data from background process to another activity

旧街凉风 提交于 2019-12-13 16:27:22

问题


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.


回答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.



来源:https://stackoverflow.com/questions/20463590/how-to-pass-the-data-from-background-process-to-another-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!