detect whether wear app started with voice command or touch input

▼魔方 西西 提交于 2019-12-04 08:36:25

After checking the activity log a bit, I found this:

When you click on an app in the Android Wear, this is logged:

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 start the app with 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 is the cat or category parameter which includes android.intent.category.LAUNCHER as a value.

The following code in the onCreate function will differentiate whether the app is started with voice or by a user tap.

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 app scenario and hope it works for others too.

Taking the answer from dhaval, I see that the category LAUNCHER is set when launching from a third-party launcher such as Wear Mini Launcher.

So instead, the following check will currently work (though may change in future Wear versions):

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");
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!