请原谅我,如果我完全忽略了这里的明显,但我似乎无法从代码中找出如何区分smartWatch 1和smartWatch 2.硬件和显示尺寸似乎有些差异我想说明这一点。 Soo ...如果有人知道如何获得当前的手表显示尺寸,或者确定当前的手表是SmartWatch 1还是2,我会非常感激!!! \ / p>
这是我尝试过的,但是对于这两款手表来说,似乎总是返回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);
}
答案 0 :(得分:1)
查看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
,那么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
传递给它,因此它可以知道宽度和高度。
答案 1 :(得分:0)
要获取SmartWatch 1的屏幕尺寸,您需要使用R.dimen.smart_watch_control_width
作为宽度,R.dimen.smart_watch_control_height
作为高度。
答案 2 :(得分:0)
好的,我刚刚找到了一种方法来完成这项工作!我打开了我的SampleExtensionService.java文件,并从CreateControlExtension函数中捕获了hotAppPackageName。基本上我更新了SampleExtensionService.java,就像这样工作:
public static String hostPackage = "";
@Override
public ControlExtension createControlExtension(String hostAppPackageName) {
hostPackage = hostAppPackageName;
return new SampleSensorControl(hostAppPackageName, this);
}
现在我只需从SampleExtenionService调用hostPackage:
String pkg = SampleExtensionService.hostAppPackageName
现在返回com.sonymobile.smartwatch2或com.sonyericsson.extras.smartwatch
似乎对我有用!!希望能帮到别人!
------------编辑-------------
好的,所以我现在觉得自己像个白痴!我正在创建一个控件扩展,在初始化步骤中就是hostAppPackageName
SampleSensorControl(final String hostAppPackageName, final Context context) {
: - 0 !!!!!我会留下我的初步答案,以防它对其他人有用。