2015-07-02 38 views
0

我有一个Android应用程序,它有一个运行线程的类。基本上与here一样。Android应用程序 - 简历线程

当前的线程每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(); 
     } 
} 
+0

将onDestroy方法设置运行标志为false – SKT

+0

您需要显示代码在哪里以及如何创建新的'Thread'以及创建'runnable'的位置。 – eleven

+0

添加了代码,我在那里创建新的线程 –

回答

1

您可以扩展应用类别及插入有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); 

最后,你需要修改的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(); 
     } 
    } 

它应该工作,也许通过一些小的变化。

+0

您的答案看起来很有希望,谢谢!但是我还没有完全理解它。我有2个类(见问题编辑)。我如何以及在哪里准确地存储(设置)我的活动,以便在应用程序恢复后我需要以后再获取? –

+0

我为你编辑并添加了一些代码 – yshahak

+0

哇,谢谢!这比我想象的要复杂得多。我目前正在重构一些东西来尝试一下。 (我扩展了“活动”而不是“应用程序”,所以这个解决方案不会在开箱即可使用)。 –