[android]线程类并将信息传递给主 activity 类成TextView

发布时间: 2017/3/19 18:06:10
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想要跑进线程的方法 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 类 (不 activity 或什么)

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:

有多种方法可以实现写作上 textview ,以下是一个办法。(您可能想要阅读有关回调,检查如何实现 Java 中的回调)。

至于从那里的背景,多种方法来做得一样好访问 UI 线程 (检查︰运行在另一个线程从主线程中的代码)。

为什么我们使用 HandlerThread 下面,你可以在这里读到︰ Acclerometer Sensor 在单独线程

所以你的听众就变成︰

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

activity :

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

此外,当您重写生命周期方法的activity如 onStartonResume 等等......,请确保调用 super.lifeCycleMethod


赞助商