gpt4 book ai didi

java - 线程类并将信息传递到主 Activity 类到 TextView 中

转载 作者:行者123 更新时间:2023-12-01 08:57:55 26 4
gpt4 key购买 nike

我想让 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) {

}
}

最佳答案

有多种方法可以实现在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);
}

您的 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 的 LifeCycle 方法(例如 onStartonResume 等)时,请确保调用 super.lifeCycleMethod.

<小时/>

关于java - 线程类并将信息传递到主 Activity 类到 TextView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41921306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com