Wake up Android Wear from a service when in sleep mode?

2.4k views Asked by At

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.

2

There are 2 answers

1
Michał Tajchert On

I would avoid Xamarin for Wear as it might cause some conflicts and not work well as it wasn't designed to.

Also if you created Service that is triggered on Android Wear - I would say Message API is correct way to do it, you should have no problem with a creating notification (on a watch itself) with vibrations (without need to touch screen) and update notification from your Service - to update timer value.

No wake locks needed.

Might help: How can I create notification that is different on device and wear?

0
Maciek Kwapinski On

I've done something like that on mobile just by adding one line in onCreate of the activity i was calling by service.

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //add this below getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); }