Problem with starting Wear apps on an idle watch

If you have an Android app running on a phone that needs to start a Wear app on a smart watch then you’ll have a problem if the Wear device is in an idle state.

For example if the phone app uses this bit of code in order to start the Wear app (by launching a new activity):

Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), START_ACTIVITY_PATH, data);

Then if the watch screen is in idle mode and the app isn’t already running then the app won’t launch. On the LG G Watch there are no error messages shown on the screen, it just jumps to displaying the “Home screen”. If the screen was already unlocked then the Wear app would start normally.

The workaround for this involved starting another activity on the watch briefly before the main activity to turn the screen on. This couldn’t do be done from the main app for an unknown reason.

So in terms  of coding, I set up a new Window Activity:

import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;

public class WindowActivity extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    super.finish();
  }
}

And then made sure I called it in the WearableListenerService’s onMessageReceived method before trying to open the Wear app:

 if (path.equals(START_ACTIVITY_PATH)) {
   if (!started) {
     // reactivate the screen
     Intent startWindow = new Intent(this, WindowActivity.class);
     startWindow.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(startWindow);

     // now open the Wear app
     Intent startWatch = new Intent(this, Watch.class);
     startWatch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(startWatch);
   }
   else {
     // screen already started so just open Wear app
     Intent startWatch = new Intent(this, Watch.class);
     startWatch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(startWatch);
   }
 }