Determine if the wearing app has started using voice command or touch input

In my download application, I would like to show various layout options based on whether the application starts via voice command or through touch input. But I could not find a way to do this in the docs.

The only possible way I can think of is to have two launchers. But I'm looking for a fairly direct solution than creating multiple launchers.

+5
source share
2 answers

After checking the activity log, I found the following:

When you click on an app in Android Wear, it logs in:

I / ActivityManager (446): START u0 {act = android.intent.action.MAIN flg = 0x10000000 cmp = com.lge.wearable.compass / .MainActivity} from uid 10002 on display 0

When you launch the application using a voice command, this is logged:

I / ActivityManager (446): START u0 {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] flg = 0x10008000 pkg = com.lge.wearable.compass cmp = com.lge.wearable. compass / .MainActivity} from uid 10002 on display 0

The difference lies in the cat or category parameter, which includes android.intent.category.LAUNCHER as the value.

The following code in the onCreate function will distinguish whether the application is launched by voice or by pressing a button.

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); .... Set<String> categories = getIntent().getCategories(); if(categories != null && categories.contains(Intent.CATEGORY_LAUNCHER)) { Log.i(LOGTAG, "app started via voice"); }else{ Log.i(LOGTAG, "app started with user tap"); } .... } 

This currently works for my application script and hope it works for others as well.

+3
source

Accepting a response from Dhaval, I see that the grenade launcher category is set at startup with a third party launcher, such as a Mini Launcher wear.

So instead, the following check is currently being performed (although it may change in future versions of Wear):

 int flags = getIntent().getFlags(); String pkg = getIntent().getPackage(); if(pkg != null && (flags & Intent.FLAG_ACTIVITY_CLEAR_TASK) > 0) { Log.i("MUSICCONTROL", "app started via voice"); }else{ Log.i("MUSICCONTROL", "app started with user tap"); } 
0
source

Source: https://habr.com/ru/post/1209071/


All Articles