}
}
- AbilitySlice
文章图片
文章图片
- 名词差异
log.csdn.net/fjnu_se/article/details/112170665)5. 传感器使用
- 权限配置
“abilities”: [
{
“skills”: [
{
“entities”: [
“entity.system.home”
],
“actions”: [
“action.system.home”
]
}
],
“orientation”: “unspecified”,
“name”: “com.example.myapplication.MainAbility”,
“icon”: “$media:icon”,
“description”: “$string:mainability_description”,
“label”: “MyApplication”,
“type”: “page”,
“launchType”: “standard”
}
],
“reqPermissions”: [
{
“name”: “ohos.permission.ACCELEROMETER”,//授予加速器权限
“reason”: “”,
“usedScene”: {
“ability”: [
“.MainAbility”
],
“when”: “inuse”
}
}
]
- 权限检查和请求
public void onStart(Intent intent) {
super.onStart(intent);
if (verifySelfPermission(“ohos.permission.ACTIVITY_MOTION”) != 0) {
if (canRequestPermission(“ohos.permission.ACTIVITY_MOTION”)) {
requestPermissionsFromUser(new String[] {“ohos.permission.ACTIVITY_MOTION”}, 1);
}
}
// …
}
@Override
public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions,
int[] grantResults) {
switch (requestCode) {
case 1: {
// 匹配requestPermissionsFromUser的requestCode
if (grantResults.length > 0 && grantResults[0] == 0) {
// 权限被授予
} else {
// 权限被拒绝
}
return;
}
}
}
- 使用传感器
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_sensor_layout);
findComponent(rootComponent);
// 创建传感器回调对象。
orientationDataCallback = new ICategoryOrientationDataCallback() {
@Override
public void onSensorDataModified(CategoryOrientationData categoryOrientationData) {
int dim = categoryOrientationData.getSensorDataDim(); //获取传感器的维度信息
float degree = categoryOrientationData.getValues()[0]; // 获取传感器的第一维数据
float[] rotationMatrix = new float[matrix_length];
CategoryOrientationData.getDeviceRotationMatrix(rotationMatrix, categoryOrientationData.values); // 根据传感器的数据获得旋转矩阵
float[] rotationAngle = new float[rotationVectorLength];
rotationAngle = CategoryOrientationData.getDeviceOrientation(rotationMatrix, rotationAngle); // 获取设备的方向
}
@Override
public void onAccuracyDataModified(CategoryOrientation categoryOrientation, int i) {
// 使用变化的精度
}
@Override
public void onCommandCompleted(CategoryOrientation categoryOrientation) {
// 传感器执行命令回调
}
};
btnSubscribe.setClickedListener(v -> {
// 获取传感器对象,并获取传感器数据
orientationSensor = categoryOrientationAgent.getSingleSensor(
CategoryOrientation.SENSOR_TYPE_ORIENTATION);
if (orientationSensor != null) {
categoryOrientationAgent.setSensorDataCallback(
orientationDataCallback, orientationSensor, interval);
}
});
// 取消获取传感器数据
btnUnsubscribe.setClickedListener(v -> {
if (orientationSensor != null) {
categoryOrientationAgent.releaseSensorDataCallback(
orientationDataCallback, orientationSensor);
}
});
}
private void findComponent(Component component) {
btnSubscribe = (Button) component.findComponentById(Resource.Id.btnSubscribe);
btnUnsubscribe = (Button) component.findComponentById(Resource.Id.btnUnsubscribe);
}
6. 常见问题
- Error Deploying HAP
文章图片
- 同时安装DevEcoStudio1.0,DevEcoStudio2.0时报错
7. 注意事项
DevEco Studio 1.0 和2.0 不同,开发鸿蒙应用使用2.0,且对比1.0缺失部分功能,如平行视界等,我本来想试试平行视界,鼓捣了半天才发现只有DevEco Studio 1.0有MagicWindow
【程序员|DevEcoStudio的及其传感器的使用,闭关在家37天“吃透”这份345页PDF】
文章图片