[Android SDK] WearableListenerService crashes when sending event

This little nightmare has made me waste several hours. Let’s explain it.

Well, the root cause is that the only Lollipop (5.0.1) device that I own in this moment hasn’t got a proper USB driver, so I’m not able to debug step-by-step on it with ADB.

I was trying to control a ViewPager from my Wearable; There are two buttons in the clock (next, previous), and there is a viewPager on the phone.

When clicking “Next”, viewPager goes to next item, when clicking “Previous” on Wearable, phone goes to previous item.

I was wondering why the App was crashing every time I tried to modify the viewPager, but not when displaying a Toast.

The reason is simple:

WearableListenerService does not run on the UI Thread.

So, everytime I tried to do viewPager.setCurrentItem(), app crashed. Due to the lack of USB drivers, I was not able to trace this 😦

Solution: Easy

viewPager.post(new Runnable() {
    public void run() {
        if(viewPager.getCurrentItem() > 0)
            viewPager.setCurrentItem(viewPager.getCurrentItem()-1, true);

I hate wasting time on these silly mistakes 😦 hope you guys don’t  !!

Nice coding!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s