}
}
-
AbilitySlice
鸿蒙项目中的Ability类似android的Activity,但是分为Ability和AbilitySlice,如我们最初的helloworld项目中的MainAbility和MainAbilitySlice分别继承了Ability和AbilitySlice,MainAbility中通过setMainRoute调用MainAbilitySlice的setUIContent显示页面,做个比喻Ability相当于身体而AbilitySlice则为皮肤
-
名词差异
为了方便android开发者转至鸿蒙,故其中的名词大多和android相差无几,不过还是有些许差异,鸿蒙文件目录中的entry类似于app,项目中的ability类似于activity,onStart类似于onCreate,resourse中的element类似于values
log.csdn.net/fjnu_se/article/details/112170665)5. 传感器使用
-
权限配置
需要在config.json中先进行配置,注意reqPermissions与abilities为同级
“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”
}
}
]
-
权限检查和请求
@Override
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;
}
}
}
-
使用传感器
@Override
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
config.json中存在deviceType,可以更改它的值来适应不同设备,如wearable指智能手表,phone指手机,car指车载设备,忘记更改可能会出现以下错误
-
同时安装DevEcoStudio1.0,DevEcoStudio2.0时报错
同时安装多个DevEcoStudio版本时需要选择新的安装路径,并修改新安装DevEco Studio的“idea.properties”中的缓存路径
7. 注意事项
DevEco Studio 1.0 和2.0 不同,开发鸿蒙应用使用2.0,且对比1.0缺失部分功能,如平行视界等,我本来想试试平行视界,鼓捣了半天才发现只有DevEco Studio 1.0有MagicWindow
|