如果我完全忽略了这里的明显内容,请原谅我,但是我似乎无法从代码中找出如何区分smartWatch 1和smartWatch2。在硬件和显示尺寸上似乎有些差异,我想来说明这一点。太...如果有人知道如何获取当前手表的显示尺寸,或者确定当前手表是SmartWatch 1还是2,我将不胜感激!
这是我尝试过的方法,但对于两块手表,它似乎总是返回220x176
public static int getSupportedControlWidth(Context context) {
return context.getResources().getDimensionPixelSize(R.dimen.smart_watch_2_control_width);
}
public static int getSupportedControlHeight(Context context) {
return context.getResources().getDimensionPixelSize(R.dimen.smart_watch_2_control_height);
}
最佳答案
查看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;
}
}
编辑您必须告诉系统要支持smartwatch 2。
在您的
RegistrationInformation
类中:@Override
public int getTargetControlApiVersion() {
return 2;
}
如果是
1
,则对于isSmartWatch2ApiAndScreenDetected
您只会得到false。编辑第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
传递给它,以便它可以知道宽度和高度。