新聞中心
場(chǎng)景介紹

創(chuàng)新互聯(lián)建站是專業(yè)的同心網(wǎng)站建設(shè)公司,同心接單;提供網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行同心網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
- 通過方向傳感器數(shù)據(jù),可以感知用戶設(shè)備當(dāng)前的朝向,從而達(dá)到為用戶指明方位的目的。
- 通過重力和陀螺儀傳感器數(shù)據(jù),能感知設(shè)備傾斜和旋轉(zhuǎn)量,提高用戶在游戲場(chǎng)景中的體驗(yàn)。
- 通過接近光傳感器數(shù)據(jù),感知距離遮擋物的距離,使設(shè)備能夠自動(dòng)亮滅屏,達(dá)到防誤觸目的。
- 通過氣壓計(jì)傳感器數(shù)據(jù),可以準(zhǔn)確的判斷設(shè)備當(dāng)前所處的海拔。
- 通過環(huán)境光傳感器數(shù)據(jù),設(shè)備能夠?qū)崿F(xiàn)背光自動(dòng)調(diào)節(jié)。
接口說明
HarmonyOS 傳感器提供的功能有:查詢傳感器的列表、訂閱/取消訂閱傳感器數(shù)據(jù)、查詢傳感器的最小采樣時(shí)間間隔、執(zhí)行控制命令。
以訂閱方向類別的傳感器數(shù)據(jù)為例,本節(jié)示例涉及的接口如下:
| 接口名 | 描述 |
|---|---|
| getAllSensors() | 獲取屬于方向類別的傳感器列表。 |
| getAllSensors(int) | 獲取屬于方向類別中特定類型的傳感器列表。 |
| getSingleSensor(int) | 查詢方向類別中特定類型的默認(rèn)sensor(如果存在多個(gè)則返回第一個(gè))。 |
| setSensorDataCallback(ICategoryOrientationDataCallback, CategoryOrientation, long) | 以設(shè)定的采樣間隔訂閱給定傳感器的數(shù)據(jù)。 |
| setSensorDataCallback(ICategoryOrientationDataCallback, CategoryOrientation, long, long) | 以設(shè)定的采樣間隔和時(shí)延訂閱給定傳感器的數(shù)據(jù)。 |
| releaseSensorDataCallback(ICategoryOrientationDataCallback, CategoryOrientation) | 取消訂閱指定傳感器的數(shù)據(jù)。 |
| releaseSensorDataCallback(ICategoryOrientationDataCallback) | 取消訂閱的所有傳感器數(shù)據(jù)。 |
| 接口名 | 描述 |
|---|---|
| getSensorMinSampleInterval(int) | 查詢給定傳感器的最小采樣間隔。 |
| runCommand(int, int, int) | 針對(duì)某個(gè)傳感器執(zhí)行命令,刷新傳感器的數(shù)據(jù)。 |
開發(fā)步驟
權(quán)限配置
如果設(shè)備上使用了[表2]中的傳感器,需要請(qǐng)求相應(yīng)的權(quán)限,開發(fā)者才能獲取到傳感器數(shù)據(jù)。
| 敏感級(jí)別 | 傳感器 | HarmonyOS權(quán)限名 | 權(quán)限描述 |
|---|---|---|---|
| system_grant | 加速度傳感器、加速度未校準(zhǔn)傳感器、線性加速度傳感器 | ohos.permission.ACCELEROMETER | 允許訂閱Motion組對(duì)應(yīng)的加速度傳感器的數(shù)據(jù)。 |
| user_grant | 計(jì)步器 | ohos.permission.ACTIVITY_MOTION | 允許訂閱運(yùn)動(dòng)狀態(tài)。 |
開發(fā)者需要在 config.json 里面配置權(quán)限:
- 開發(fā)者如果需要獲取加速度的數(shù)據(jù),需要進(jìn)行如下權(quán)限配置。
"reqPermissions": [
{
"name": "ohos.permission.ACCELEROMETER",
"reason": "",
"usedScene": {
"ability": [
".MainAbility"
],
"when": "inuse"
}
}
]- 對(duì)于需要用戶授權(quán)的權(quán)限,如計(jì)步器傳感器,需要進(jìn)行如下權(quán)限配置。
"reqPermissions": [
{
"name": "ohos.permission.ACTIVITY_MOTION",
"reason": "",
"usedScene": {
"ability": [
".MainAbility"
],
"when": "inuse"
}
}
]由于敏感權(quán)限需要用戶授權(quán),因此,開發(fā)者在應(yīng)用啟動(dòng)時(shí)或者調(diào)用訂閱數(shù)據(jù)接口前,需要調(diào)用權(quán)限檢查和請(qǐng)求權(quán)限接口。
@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) {
// 權(quán)限被授予
} else {
// 權(quán)限被拒絕
}
return;
}
}
}使用傳感器
以使用方向類別的傳感器為例,運(yùn)動(dòng)類、環(huán)境類、健康類等類別的傳感器使用方法類似。
- 獲取待訂閱數(shù)據(jù)的傳感器。
- 創(chuàng)建傳感器回調(diào)。
- 訂閱傳感器數(shù)據(jù)。
- 接收并處理傳感器數(shù)據(jù)。
- 取消訂閱傳感器數(shù)據(jù)。
private Button btnSubscribe;
private Button btnUnsubscribe;
private CategoryOrientationAgent categoryOrientationAgent = new CategoryOrientationAgent();
private ICategoryOrientationDataCallback orientationDataCallback;
private CategoryOrientation orientationSensor;
private long interval = 100000000;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_sensor_layout);
findComponent(rootComponent);
// 創(chuàng)建傳感器回調(diào)對(duì)象。
orientationDataCallback = new ICategoryOrientationDataCallback() {
@Override
public void onSensorDataModified(CategoryOrientationData categoryOrientationData) {
// 對(duì)接收的categoryOrientationData傳感器數(shù)據(jù)對(duì)象解析和使用
int dim = categoryOrientationData.getSensorDataDim(); //獲取傳感器的維度信息
float degree = categoryOrientationData.getValues()[0]; // 獲取方向類傳感器的第一維數(shù)據(jù)
}
@Override
public void onAccuracyDataModified(CategoryOrientation categoryOrientation, int i) {
// 使用變化的精度
}
@Override
public void onCommandCompleted(CategoryOrientation categoryOrientation) {
// 傳感器執(zhí)行命令回調(diào)
}
};
btnSubscribe.setClickedListener(v -> {
// 獲取傳感器對(duì)象,并訂閱傳感器數(shù)據(jù)
orientationSensor = categoryOrientationAgent.getSingleSensor(
CategoryOrientation.SENSOR_TYPE_ORIENTATION);
if (orientationSensor != null) {
categoryOrientationAgent.setSensorDataCallback(
orientationDataCallback, orientationSensor, interval);
}
});
// 取消訂閱傳感器數(shù)據(jù)
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);
} 分享標(biāo)題:創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OS傳感器開發(fā)指導(dǎo)
本文地址:http://www.dlmjj.cn/article/cojgoop.html


咨詢
建站咨詢
