繁体   English   中英

Android应用程序-简历线程

[英]Android Application - Resume Thread

我有一个运行有线程的类的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();
        }
}

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

<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();
        }
    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM