恢复应用程序时重置线程

时间:2013-12-09 13:23:56

标签: java android multithreading arduino

我需要帮助!! 我创建了一个应用程序,通过单独的线程(ReadingProcessor)从arduino读取数据并将值填充到读数[],然后我创建了另一个检查值的单独线程。在此检查中,如果是第一次发生警告,则应用程序发送消息,否则如果有先前的警告读数,则应用程序应等到通过警告间隔

public class WarningProcessor extends Thread {

float readings[];
float[] min, max;
long elapsedTime;
long[] lastWarningTime;
boolean[] inWarning;
long checkInterval = Long.parseLong(Settings.Swarning) * 60000;
long currentTime;
SerialActivity sa = new SerialActivity();

WarningProcessor(float readings[]) {
    this.readings = readings;
}

@Override
public void run() {
    sleep_s(2);
    synchronized (readings) {
        lastWarningTime = new long[readings.length];
        inWarning = new boolean[readings.length];
        Arrays.fill(inWarning, false);
    }
    while (true) {
        this.readings = ReadingProcessor.readings;
        synchronized (readings) {
            for (int i = 0; i < readings.length; i++) {
                currentTime = Calendar.getInstance().getTimeInMillis();
                if (readings[i] > 100) { //here to make boundaries
                    if (inWarning[i] == false) { 
                        //send warning
                        for(String number : StartPage.phoneNumbers)
                         SmsManager.getDefault().sendTextMessage(number,
                                 null,"Warning "+readings[i], null, null);      
                        lastWarningTime[i] = currentTime;
                        inWarning[i] = true;
                    } else {
                        if (currentTime - lastWarningTime[i] > checkInterval) {
                            //send warning
                             for(String number : StartPage.phoneNumbers)
                             SmsManager.getDefault().sendTextMessage(number,
                             null,"Warning "+readings[i], null, null);
                            lastWarningTime[i] = currentTime;
                        }
                    }

                } else {
                    inWarning[i] = false;
                }
            }
        }
        sleep_s(1);
    }
}

如果是连续警告数据,程序应该按间隔发送消息,这在我仍然处于活动状态时以及当我处于暂停()状态时效果很好,但问题是在onpause()之后我返回应用程序UI,程序在连续间隔的情况下重新发送消息,丢弃等待直到通过间隔

public class SerialActivity extends Activity {

private static ArduinoSerialDriver sDriver;
private static TextView mDumpTextView;
private static ScrollView mScrollView;
String Data[]={"Temperature"};
float[] readings = new float[Data.length];
ReadingProcessor readingProcessor;
WarningProcessor warningProcessor;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.serialactivity);
    mDumpTextView = (TextView) findViewById(R.id.consoleText);
    mScrollView = (ScrollView) findViewById(R.id.demoScroller);}






@Override
protected void onStart() {
    super.onStart();
    ReadingProcessor rp = new ReadingProcessor(readings,sDriver);
    readingProcessor=rp;
    WarningProcessor wp = new WarningProcessor(readings);
    warningProcessor=wp;
    rp.start();
    wp.start();
}


@SuppressWarnings("deprecation")
@Override
protected void onDestroy() {
    super.onDestroy();
    readingProcessor.Stop();
    warningProcessor.stop();
}

所以请帮助我,我尝试了太多的解决方案,比如使用处理程序,我遇到了同样的问题

1 个答案:

答案 0 :(得分:0)

每次将应用程序返回到前台时都会调用 onStart。您的问题是每个线程都有多个实例在运行。如果您只想运行每个线程的一个实例,则需要在onCreate而不是onStart中创建和启动线程。一般情况下,如果你要在onPause中杀死它,你应该只在onStart中启动一个线程。