lundi 11 juillet 2016

Android repeating task with timeout in Java

I've a problem with repeated tasks.

Basically, I'm working on service, which sends sms and check response for a minute. If response received, I update textview with success message otherwise fail. My send sms service works ok, but I am stuck at receiving the sms. I call send sms and check sms like this:

sendSms("6617", "Test") // it works;
readSms.run() // it works too;
if (message.equals("desired sms"){ // it doesn't wait read sms to finish
    updateTextView("Success");
}
else{
    updateTextView("Fail");
}

Here is readSms:

Runnable readSms = new Runnable(){
    receivedMessage = "";
    @Override
    public void run() {
        try {
            //..checking sms..//
            if (smsreceived) {message=receivedMessage;}
        } finally {
            mHandler.postDelayed(readSms, mInterval);
        }
    }
};

How can I make readSms to wait 60 seconds timeout with 1 second interval. If sms received, I should update textview with success, if not I'll wait until timeout and set textview with fail.

Aucun commentaire:

Enregistrer un commentaire