ckn
ckn

Reputation: 141

Start an app (like Flashlight) on watch with tap on watch face

This might be a really simple question, but I can't seem to find an answer to it.

I created a watch face for Wear OS 2.0 and all I need to do is start an app like Flashlight or some other third party app on the watch when I tap the watch face. I know the code has to go here:

@Override
     public void onTapCommand(int tapType, int x, int y, long eventTime) {
         switch (tapType) {
             case TAP_TYPE_TAP:
                 **Code to start an app goes here**
                 break;
             default:
                 super.onTapCommand(tapType, x, y, eventTime);
                 break;
         }
         invalidate();

I'm also pretty sure that I have to use an Intent and startActivity() to do it. I just have no idea how.

Any help or code sample is appreciated.

Upvotes: 0

Views: 112

Answers (1)

ckn
ckn

Reputation: 141

Here is what I found so far. Thanks to Mihai Coman for pointing me in the right direction. Using the Flashlight example from my original post:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName("com.google.android.clockwork.flashlight", "com.google.android.clockwork.flashlight.FlashlightActivity");
intent.setComponent(cn);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;
if (isIntentSafe) {
     startActivity(intent);
}

All one really has to do (and struggle with) is to find the exact package and class name to be launched.

The last six lines of code are essentially just to make sure the Intent gets received and will not crash the app.

Upvotes: 2

Related Questions