I am having trouble identifying a problem on Android TimerTask (I believe) that is driving me crazy,
The scenario is: I use a PhoneStateListener to start a timer after i make a call, and after certain call time reproduce a sound.
Everything works perfect but as soon as I’m not on debug mode (or simply with the phone plugged to the computer), the Sound not always sound, most of these times it doesn’t sound by the way.
My biggest problem is that I cannot identify clearly the problem because this only happens when the phone is unplugged and i cannot read the logs. I could only reproduce the issue once (no sound after call) and read the logs about it, where I realized that: onCallStateChanged was being called, the Timer was Ok, but the TimerTask was never called (or scheduled).
So, after reading some questions here, I used a Handler instead of a Timer. But i have the exact same issues. In fact, with the Handler this issue is much easier to reproduce by simply plugging and unplugging the phone (Using Timer it was more difficult to reproduce the no-sound issue).
Am I doing things wrong or do I need to implement something else?
Any comment will be appreciated,
Thanks in advance
Edit
Apparently , with the Handler this happens because the phone goes to sleep as soon as the call begins. However using wake_lock doesn’t work to prevent this, is there any other alterlative to run a thread when phone is sleep?
Well The problem with this, as I said, was that the phone stopped timer when going to sleep during the call. I ended using the AlarmManager.
It is important to note that the AlarmReceiver must be a public class or else the receive method will never be called.
However, I can’t still understand why Timer worked sometimes and sometimes not.
Regards