查看 SampleControlExtension 项目,看看它是如何使用的:
DeviceInfoHelper.isSmartWatch2ApiAndScreenDetected()
但是,如果您愿意,您可以从任何地方调用它。
这就是 SampleExtensionService 决定 SW1 或 SW2 的方式:
@Override
public ControlExtension createControlExtension(String hostAppPackageName) {
// First we check if the API level and screen size required for
// SampleControlSmartWatch2 is supported
boolean advancedFeaturesSupported = DeviceInfoHelper.isSmartWatch2ApiAndScreenDetected(
this, hostAppPackageName);
if (advancedFeaturesSupported) {
return new SampleControlSmartWatch2(hostAppPackageName, this, new Handler());
} else {
// If not we return an API level 1 control based on screen size
final int controlSWWidth = SampleControlSmartWatch.getSupportedControlWidth(this);
final int controlSWHeight = SampleControlSmartWatch.getSupportedControlHeight(this);
final int controlSWHPWidth = SampleControlSmartWirelessHeadsetPro
.getSupportedControlWidth(this);
final int controlSWHPHeight = SampleControlSmartWirelessHeadsetPro
.getSupportedControlHeight(this);
for (DeviceInfo device : RegistrationAdapter.getHostApplication(this,
hostAppPackageName)
.getDevices()) {
for (DisplayInfo display : device.getDisplays()) {
if (display.sizeEquals(controlSWWidth, controlSWHeight)) {
return new SampleControlSmartWatch(hostAppPackageName, this, new Handler());
} else if (display.sizeEquals(controlSWHPWidth, controlSWHPHeight)) {
return new SampleControlSmartWirelessHeadsetPro(hostAppPackageName, this,
new Handler());
}
}
}
throw new IllegalArgumentException("No control for: " + hostAppPackageName);
}
}
就个人而言,我觉得没有必要使用资源,所以我选择这样做。我有一个enum
定义,我使用与上述查询类似的代码,isSmartWatch2ApiAndScreenDetected
然后传递正确的枚举值。
import android.graphics.Bitmap.Config;
public enum ScreenConfiguration {
SMARTWATCH1(128, 128, Config.RGB_565), SMARTWATCH2(220, 176, Config.RGB_565);
private final int mWidth;
private final int mHeight;
private final Config mBitmapConfig;
private ScreenConfiguration(int width, int height, Config bitmapConfig) {
mWidth = width;
mHeight = height;
mBitmapConfig = bitmapConfig;
}
public int getWidth() {
return mWidth;
}
public int getHeight() {
return mHeight;
}
public Config getBitmapConfig() {
return mBitmapConfig;
}
}
编辑您必须告诉系统您要支持智能手表 2。
在你的RegistrationInformation
课堂上:
@Override
public int getTargetControlApiVersion() {
return 2;
}
如果那是1
,你只会得到 false isSmartWatch2ApiAndScreenDetected
。
编辑第 2 部分如何使用枚举
@Override
public ControlExtension createControlExtension(String hostAppPackageName) {
// First we check if the API level and screen size required for
// SampleControlSmartWatch2 is supported
boolean advancedFeaturesSupported = DeviceInfoHelper.isSmartWatch2ApiAndScreenDetected(
this, hostAppPackageName);
if (advancedFeaturesSupported) {
return new SampleControlSmartWatch(ScreenConfiguration.SMARTWATCH2, hostAppPackageName, this, new Handler());
} else {
// If not we return an API level 1 control based on screen size
final int controlSWWidth = SampleControlSmartWatch.getSupportedControlWidth(this);
final int controlSWHeight = SampleControlSmartWatch.getSupportedControlHeight(this);
final int controlSWHPWidth = SampleControlSmartWirelessHeadsetPro
.getSupportedControlWidth(this);
final int controlSWHPHeight = SampleControlSmartWirelessHeadsetPro
.getSupportedControlHeight(this);
for (DeviceInfo device : RegistrationAdapter.getHostApplication(this,
hostAppPackageName)
.getDevices()) {
for (DisplayInfo display : device.getDisplays()) {
if (display.sizeEquals(controlSWWidth, controlSWHeight)) {
return new SampleControlSmartWatch(ScreenConfiguration.SMARTWATCH1, hostAppPackageName, this, new Handler());
} else if (display.sizeEquals(controlSWHPWidth, controlSWHPHeight)) {
return new SampleControlSmartWirelessHeadsetPro(hostAppPackageName, this,
new Handler());
}
}
}
throw new IllegalArgumentException("No control for: " + hostAppPackageName);
}
}
大部分与第一个示例相同,但看看我如何使用相同的控件类SampleControlSmartWatch
并将其传递ScreenConfiguration
enum
给它,因此它可以知道宽度和高度。