线程类并将主要活动类的信息传递给TextView

时间:2017-01-29 13:16:31

标签: java android multithreading

我想让onSenzorChange方法运行到线程中。跑得更顺畅。并且每次改变都获得x轴的信息。并将它传递给主类(Activity)到TextView。

MainActivity类:

public class MainActivity extends AppCompatActivity {
    TextView textView;
    TestOfPassUIThread t;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);

        t = new TestOfPassUIThread(this);
    }
    public void onStart(View view) {
        t.register();
    }
}

TestOfPassUIThread类(不是活动或任何东西)

public class TestOfPassUIThread implements SensorEventListener {

    private SensorManager sensorManager;
    private Sensor sensor;

    public TestOfPassUIThread (Context context) {
        sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    }
    public void register(){
        sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    }
    public void unregister() {
        sensorManager.unregisterListener(this);
    }
    // Want this method be in Thread
    //How can I do this ?
    @Override
    public void onSensorChanged(SensorEvent event) {
        float xAxis = event.values[0];
        // And I want it to display in TextView!
        // In main activity would be textView.setText("" + xAxis);
        //How to pass it to MainActivity class ?
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}

1 个答案:

答案 0 :(得分:1)

有多种方法可以在textview上实现写作,以下是一种方法。 (您可能想了解回调,请查看How to implement callbacks in Java)。

至于从后台访问UI线程,还有多种方法(检查:Running code in main thread from another thread)。

为什么我们使用下面的HandlerThread,您可以在此处阅读:Acclerometer Sensor in Separate Thread

所以你的听众变成了:

public abstract class TestOfPassUIThread implements SensorEventListener {

    private SensorManager   sensorManager;
    private Sensor          sensor;
    private HandlerThread   handlerThread;
    private Context         context;
    private Runnable        uiRunnable;

    private float           xAxis;

    public TestOfPassUIThread (Context context) {
        this.context    = context;
        sensorManager   = (SensorManager) context.getSystemService (Context.SENSOR_SERVICE);
        sensor          = sensorManager.getDefaultSensor (Sensor.TYPE_ACCELEROMETER);
    }

    public void register () {
        initUiRunnable ();

        handlerThread   = new HandlerThread ("sensorHandler");
        handlerThread.start ();

        sensorManager.registerListener (
            this,
            sensor,
            SensorManager.SENSOR_DELAY_NORMAL,
            new Handler (handlerThread.getLooper ())
        );
    }

    public void unregister () {
        sensorManager.unregisterListener (this);

        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                handlerThread.quitSafely ();
                return;
            }
            handlerThread.quit ();
        } finally {
            uiRunnable = null;
        }
    }

    @Override
    public void onSensorChanged (SensorEvent event) {
        xAxis = event.values [0];

        // your other background operations

        ((Activity)context).runOnUiThread (uiRunnable);

        // your other background operations
    }

    @Override
    public void onAccuracyChanged (Sensor sensor, int accuracy) {

    }

    private void initUiRunnable () {
        uiRunnable = new Runnable () {

            @Override
            public void run () {
                // ...... your other UI operations

                fillTextView (xAxis);

                // ...... your other UI operations
            }
        };
    }

    public abstract void fillTextView (float xAxis);
}

您的活动:

public class MainActivity extends AppCompatActivity {

    private TextView           textView;
    private TestOfPassUIThread t;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);

        textView = (TextView)findViewById (R.id.textView);

        t = new TestOfPassUIThread (this) {

            @Override
            public void fillTextView (float xAxis) {
                textView.setText ("Current xAxis: " + xAxis);
            }
        };
    }

    @Override
    protected void onResume () {
        super.onResume ();
        t.register ();
    }

    @Override
    protected void onPause () {
        t.unregister ();
        super.onPause ();
    }
}

此外,当您覆盖onStartonResume等活动的LifeCycle方法时,请务必致电super.lifeCycleMethod