detect whether wear app started with voice command or touch input

南楼画角 提交于 2019-12-06 03:37:38

问题


In my wear app I would like to show different layout options based on whether app is started via a voice command or via a touch input. But I could not find any way to do this in docs.

The only possible way I can think of is to have two launchers. But I am looking for rather direct solution than creating multiple launchers.


回答1:


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.




回答2:


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");
}


来源:https://stackoverflow.com/questions/27480724/detect-whether-wear-app-started-with-voice-command-or-touch-input

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