Reputation: 7659
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.
Upvotes: 5
Views: 464
Reputation: 3961
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");
}
Upvotes: 0
Reputation: 7659
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.
Upvotes: 3