I am working on Android Wear app that is somehow a timer that displays a notification when the time is elapsed and then start again. The notification displays a message on the watch and make the device vibrating.
The app is working fine except when the watch is in sleep mode. In this case, the user needs to touch the screen in order that the watch vibrates and the timer starts again.
The notification is created and displayed from a WearableListenerService. I tried many solutions such as Wake locks. The code is written in C# with Xamarin.Android but i think any java dev will also be able to read:
static PowerManager.WakeLock sWakeLock;
static object LOCK = new object();
void DisplayNotification()
{
//create the wake lock
lock (LOCK)
{
if (sWakeLock == null)
{
// This is called from BroadcastReceiver, there is no init.
var pm = (PowerManager)GetSystemService(Context.PowerService));
sWakeLock = pm.NewWakeLock(
WakeLockFlags.ScreenBright | WakeLockFlags.Full | WakeLockFlags.AcquireCausesWakeup, "My WakeLock Tag");
}
}
sWakeLock.Acquire();
//display the notification
//....
//release the wake lock
lock (LOCK)
{
//Sanity check for null as this is a public method
if (sWakeLock != null)
sWakeLock.Release();
}
}
My issue is that the watch will wake up at some point (after few minutes) but not immediately when requested. I tried to put a delay on the release of the wake lock or to make an sWakeLock.Acquire(2000)
in order that the wake lock is automatically released after 2 secs. Then, I also tried other solutions with the WakefulBroadcastReceiver
and the AlarmManager
but I still have the same behaviour.