问题
I have a mobile and wear module in Android Studio and they are both using a 'core' code-base...in that core-code, how could I determine whether my code is running on a 'wear' of 'mobile' module? Should i use screen size?
回答1:
Though this isn't officially documented (that I know of) you can determine whether the device is a watch or handheld by invoking either:
getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH);
or by using:
Configuration config = getResources().getConfiguration();
boolean isWatch = (config.uiMode & Configuration.UI_MODE_TYPE_MASK) == Configuration.UI_MODE_TYPE_WATCH;
I've tested both and they return true in my test Wear device (Moto 360) and false in a Nexus 5 phone.
(I found them by browsing the API Differences Report for API level 4.4W (20). Though not guaranteed, these should be more future-proof than checking for the non-availabilty of other features, as those might be just temporary limitations).
回答2:
As documentation says:
Wearable apps can access much of the standard Android APIs, but don't support the following APIs:
- android.webkit
- android.print
- android.app.backup
- android.appwidget
- android.hardware.usb
So you can use hasSystemFeature(String name) with those unsupported APIs (as input) to check whether the given feature name is one of the available features in your device. If it returned false
, so you'd be in wear, otherwise you'd be in a mobile.
来源:https://stackoverflow.com/questions/27406708/how-can-i-determine-if-i-am-running-in-android-wear-or-not