I have an extremely simple Android Wear app that basically is just one button on the mobile device that I want to use to turn the display of my Android Wear device on and off. I have the application sending messages to the Android Wear device to tell it to acquire or release a wakeLock depending on the screen state. The wakeLock.acquire() works perfectly fine but when I send the message to release the wakeLock the application misbehaves. Acquiring the wakeLock seems to always work but releasing it will cause one of the follwoing: it will either crash and say "Unfortunately your app has stopped working", or it won't do anything (as if it thinks I'm not holding the wakeLock so it doesn't even enter the if-statment that contains the release call) or it will see that it is held but my call to wakeLock.release() doesn't seem to actually release the lock (screen never dims). Any ideas?
Mobile Device Code:
// sendToast is the onClickListener function for my toggleButton
private void sendToast() {
if(isChecked(findViewById(R.id.toggleButton)))
message = ON_MESSAGE;
else
message = OFF_MESSAGE;
if (nodeId != null) {
new Thread(new Runnable() {
@Override
public void run() {
client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
Wearable.MessageApi.sendMessage(client, nodeId, message, null);
client.disconnect();
}
}).start();
}
}
private boolean isChecked(View view) {
if (((ToggleButton) view).isChecked())
return true;
else
return false;
}
Wear Device Code:
public class ListenerService extends WearableListenerService {
private PowerManager powerManager;
private PowerManager.WakeLock wakeLock;
private static final String ON_MESSAGE = "On!";
private static final String OFF_MESSAGE = "Off!";
boolean first = true;
private void setupWakeLock() {
powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP, "wakeLock"); // SCREEN_BRIGHT_WAKE_LOCK
}
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (first) {
setupWakeLock();
first = false;
}
Toast.makeText(this, wakeLock.toString(), Toast.LENGTH_LONG).show();
if (messageEvent.getPath().equals(ON_MESSAGE)) {
if (!wakeLock.isHeld()) {
wakeLock.acquire();
Toast.makeText(this, ON_MESSAGE, Toast.LENGTH_LONG).show();
}
} else if (messageEvent.getPath().equals(OFF_MESSAGE)) {
if (wakeLock.isHeld()) {
wakeLock.release();
Toast.makeText(this, OFF_MESSAGE, Toast.LENGTH_LONG).show();
}
}
}
private void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
I based the project off a project I found on github and changed some things around. I'm not trying to develop an app for distribution just a one-off little app for my own use. I'm using wakeLock because the Android Wear code is in a Listener Service (there is no activity so I don't have a Window to obtain and set flags on or anything). I'm testing using an Asus tablet and a Round Android Wear emulator. Turning on/off the display is such a simple function that I couldn't find support for in any third-party apps that I looked at and the fact that it is giving me this much trouble is completely maddening. Any ideas would be greatly appreciated!