gpt4 book ai didi

java - Android 应用程序 - 恢复线程

转载 作者:行者123 更新时间:2023-12-01 11:21:25 25 4
gpt4 key购买 nike

我有一个 Android 应用程序,它有一个运行线程的类。与 here 基本相同.

当前线程每 500 毫秒更新一个带有计算值的 TextView ,并另外记录该值,以便我可以在 adb-logcat 中看到它。

当我使用设备的后退按钮退出应用程序时,线程仍然在后台运行。 (这就是我想要的)。 adb-logcat 仍然给我线程正在计算的值。

但是当我重新打开应用程序时, TextView 不再更新!

当我再次打开应用程序时,我必须做什么才能恢复更新 TextView ?

这是我的简化代码:

SensorProcessor.java

public class SensorProcessor implements Runnable {

protected Context mContext;
protected Activity mActivity;

private volatile boolean running = true;
//Tag for Logging
private final String LOG_TAG = SensorProcessor.class.getSimpleName();

public SensorProcessor(Context mContext, Activity mActivity){
this.mContext = mContext;
this.mActivity = mActivity;
}


public void run() {

while (running){

try {
final String raw = getSensorValue();
mActivity.runOnUiThread(new Runnable() {
public void run() {
final TextView textfield_sensor_value;
textfield_sensor_value = (TextView) mActivity.findViewById(R.id.text_sensor);
textfield_sensor_value.setText("Sensor Value: " + raw); // <-------- Does not update the field, when app resumes
Log.v(LOG_TAG, raw); // <-------- Still working after the app resumes
}
});


Thread.sleep(100);
} catch (InterruptedException e) {
//When an interrupt is called, we set running to false, so the thread can exit nicely
running = false;
}
}

Log.v(LOG_TAG, "Sensor Thread finished");

}
}

MainActivity.java

public class MainActivity extends Activity implements OnClickListener, OnInitListener {
//Start the Thread, when the button is clicked
public void onClick(View v) {
if (v.getId() == R.id.button_start) {
runnable = new SensorProcessor(this.getApplicationContext(),this);
thread = new Thread(runnable);
thread.start();
}
}

最佳答案

您可以扩展 Application 类并将 getter 和 setter 方法插入到您的 Runnable 中。下面是 MyApplication 的示例(不要忘记添加 list 连接!),在 manifest.xml 中:

<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme" >

然后是我的应用程序:

public class MyApplication extends Application {

private SensorProcessor mSensorProcessor = null;

public SensorProcessor getCurrentSensorProcessor(){
return mSensorProcessor;
}

public void setSensorProcessor(SensorProcessor mSensorProcessor){
this.mSensorProcessor = mSensorProcessor;
}

}

在您的 Activity 的 onCreate() 内调用:

 ((MyApplication)getApplication()).getCurrentSensorProcessor().mActivity = this;

您还需要修改 Runnable 构造函数:

public SensorProcessor(Context mContext, Activity mActivity){
this.mContext = mContext;
this.mActivity = mActivity;
((MyApplication)mActivity.getApplication()).setSensorProcessor(this);
}

并且不要忘记在完成时通过在 Runnable 中调用它来清空 mSensorProcessor 的实例:

((MyApplication)mActivity.getApplication()).setSensorProcessor(null);

最后您需要修改 Activity 中的 onClick:

 if (v.getId() == R.id.button_start) {
SensorProcessor mSensorProcessor = ((MyApplication)getApplication()).getCurrentSensorProcessor();
if (mSensorProcessor != null)
mSensorProcessor.mActivity = this;
else {
runnable = new SensorProcessor(this.getApplicationContext(), this);
thread = new Thread(runnable);
thread.start();
}
}

它应该可以工作,也许需要一些小的改变。

关于java - Android 应用程序 - 恢复线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31182112/

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