Android应用程序 - 恢复线程

时间:2015-07-02 10:29:58

标签: java android multithreading

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

此刻线程每隔500毫秒更新一个带有计算值的文本视图,并另外记录该值,因此我可以在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();
        }
}

1 个答案:

答案 0 :(得分:1)

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

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

然后MyApplication:

public class MyApplication extends Application {

    private SensorProcessor mSensorProcessor = null;

     public SensorProcessor getCurrentSensorProcessor(){
        return mSensorProcessor;
     }

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

}

通过电话在您的活动的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();
        }
    }

它应该工作,也许是一些微小的变化。

相关问题