2015-07-02 40 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

哇,謝謝!這比我想象的要複雜得多。我目前正在重構一些東西來嘗試一下。 (我擴展了「活動」而不是「應用程序」,所以這個解決方案不會在開箱即可使用)。 –