I am trying to get notification whenever a new email arrives in inbox, code is given below. My application becomes irresponsive after short time, although in logCat application is still communicating with the imap server….but android system ask me to either wait or close this application. Is this because of the “thread.sleep()”? How can I solve it?
Can any one guide me? :/
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
//player.start();
//add listenser and keep listening
try{
folder.addMessageCountListener(new MessageCountAdapter() {//start
public void messagesAdded(MessageCountEvent ev) {
Message[] msgs = ev.getMessages();
// msgs.length
Log.d(TAG, "It is inside LISTENER");
}
});//end
for (; ;) {
Thread.sleep(20000); // sleep for freq milliseconds
// This is to force the IMAP server to send us
// EXISTS notifications.
Log.d(TAG, "AFTER SLEEP");
folder.getMessageCount();
}
}
catch(Exception e){
}
}
onStartis deprecated. You should useonStartCommandinstead.The system calls both
onStartandonStartCommandon the main UI Thread. The reason why your app is appearing to be “irresponsive” is because you are callingThread.sleep(20000)on the main UI thread. The main UI thread is responsible for generating your layouts and responding to touch/UI events. The call toThread.sleepis preventing all of this from happening, and thus your app appears to be hang (and eventually you get an Application Not Responding error).