简体   繁体   中英

Thread reset when resuming application

Please I need help!! I created an app that reads data from arduino through separate thread (ReadingProcessor) and fillings the values into readings[], then I created another separate thread that checks on the values. In this checking, if it's the first time that a warning occurs then the application sends message, else if there is previous warning readings, the application should wait till passing a warning interval

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

In case of continuous warning data the program should sends message in interval, and this works well when I'm still on activity and also when I'm onpause() state, but the problem is that after the onpause() when I return to application UI , the program resends messages in case of continuous interval, discarding the waiting till passing the interval

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

So please help me, I tried too many solutions like using handler and I got the same problem

onStart is called every time you return the application to the foreground. Your problem is that you have multiple instances of each thread running. If you only want one instance of each thread running, you need to create and start the threads in onCreate instead of onStart. In general, you should only start a thread in onStart if you are going to kill it in onPause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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