Die übergabe von Daten aus thread in die Aktivität

Ich soll für die übergabe der Daten von einem Thread zu Activity (die den thread).

So, ich mache wie beschrieben auf Android-Dokumentation:

public class MyActivity extends Activity {

    [ . . . ]
    //Need handler for callbacks to the UI thread
    final Handler mHandler = new Handler();

    //Create runnable for posting
    final Runnable mUpdateResults = new Runnable() {
        public void run() {
            updateResultsInUi();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        [ . . . ]
    }

    protected void startLongRunningOperation() {

        //Fire off a thread to do some work that we shouldn't do directly in the UI thread
        Thread t = new Thread() {
            public void run() {
                mResults = doSomethingExpensive();
                mHandler.post(mUpdateResults);
            }
        };
        t.start();
    }

    private void updateResultsInUi() {

        //Back in the UI thread -- update our UI elements based on the data in mResults
        [ . . . ]
    }
}

Nur eine Sache fehlt mir hier - wo und wie sollte die definiert sein mResults so ich könnte greifen Sie von beiden Activity und Thread, und würde auch in der Lage, bei Bedarf zu ändern? Wenn ich es definieren als final im MyActivity, die ich nicht ändern kann, es nicht mehr in Thread - wie es in Beispiel...

Dank!

InformationsquelleAutor Laimoncijus | 2010-06-12
Schreibe einen Kommentar