Skip to content

常见权限

汇总开发中最常用的权限及其使用方式。


1. 网络相关权限

INTERNET(网络访问)

json5
{
  "name": "ohos.permission.INTERNET",
  "reason": "$string:internet_reason"
}
// 等级:normal(自动授予)
// 用途:HTTP/HTTPS 请求、WebSocket 连接
typescript
import http from '@kit.NetworkKit';

// 无需检查,直接使用
async function fetchData() {
  let httpRequest = http.createHttp();
  let response = await httpRequest.request('https://api.example.com');
  return response.result;
}

ACCESS_NETWORK_STATE(网络状态)

json5
{
  "name": "ohos.permission.ACCESS_NETWORK_STATE",
  "reason": "$string:network_state_reason"
}
// 等级:normal
// 用途:查询网络连接类型
typescript
import { network } from '@kit.NetworkKit';

let netState = network.getDefaultNet();
console.log(`网络类型:${netState}`);

2. 相机与媒体权限

CAMERA(相机)

json5
{
  "name": "ohos.permission.CAMERA",
  "reason": "$string:camera_reason",
  "usedScene": {
    "abilities": ["EntryAbility"],
    "when": "inuse"
  }
}
// 等级:user_granted
// 用途:拍照、录像、预览
typescript
import { camera } from '@kit.ArkCamera';

async function useCamera() {
  let granted = await checkPermission('ohos.permission.CAMERA');
  if (!granted) {
    await requestPermission('ohos.permission.CAMERA');
  }
  
  // 打开相机
  let cameraManager = camera.getCameraManager(context);
  let cameras = cameraManager.getSupportedCameras();
}

READ_MEDIA / WRITE_MEDIA(媒体读写)

json5
{
  "name": "ohos.permission.READ_MEDIA",
  "reason": "$string:read_media_reason"
},
{
  "name": "ohos.permission.WRITE_MEDIA",
  "reason": "$string:write_media_reason"
}
// 等级:user_granted
// 用途:访问相册、保存文件
typescript
import { photoAccessHelper } from '@kit.ArkPhoto';

// 选择照片
async function selectPhotos() {
  let photoViewPicker = photoAccessHelper.getPhotoViewPicker();
  let result = await photoViewPicker.select({
    maxSelectNumber: 9
  });
  return result.photoUris;
}

// 保存图片
async function saveImage(imageData: ArrayBuffer) {
  let photoAccess = photoAccessHelper.getPhotoAccess();
  await photoAccess.saveImage(imageData, {
    name: 'photo.jpg',
    description: '保存的照片'
  });
}

3. 位置权限

LOCATION(定位)

json5
{
  "name": "ohos.permission.LOCATION",
  "reason": "$string:location_reason",
  "usedScene": {
    "abilities": ["EntryAbility"],
    "when": "inuse"
  }
}
// 等级:user_granted
// 用途:GPS 定位、基站定位
typescript
import { geoLocationManager } from '@kit.ArkGeoLocationManager';

async function getCurrentLocation() {
  let granted = await checkPermission('ohos.permission.LOCATION');
  if (!granted) {
    await requestPermission('ohos.permission.LOCATION');
  }
  
  let locationManager = geoLocationManager.getGeoLocationManager(context);
  let location = await locationManager.getCurrentLocation();
  console.log(`纬度:${location.latitude}, 经度:${location.longitude}`);
}

4. 音频权限

MICROPHONE(麦克风)

json5
{
  "name": "ohos.permission.MICROPHONE",
  "reason": "$string:microphone_reason",
  "usedScene": {
    "abilities": ["EntryAbility"],
    "when": "inuse"
  }
}
// 等级:user_granted
// 用途:录音、语音识别
typescript
import { audio } from '@kit.ArkAudio';

async function startRecording() {
  let granted = await checkPermission('ohos.permission.MICROPHONE');
  if (!granted) {
    await requestPermission('ohos.permission.MICROPHONE');
  }
  
  let audioRecorder = audio.createAudioRecorder();
  await audioRecorder.prepare();
  await audioRecorder.start();
}

5. 蓝牙权限

USE_BLUETOOTH(蓝牙使用)

json5
{
  "name": "ohos.permission.USE_BLUETOOTH",
  "reason": "$string:bluetooth_reason"
}
// 等级:normal(自动授予)
// 用途:蓝牙扫描、连接
typescript
import { bluetooth } from '@kit.ArkBluetooth';

// 无需申请,直接使用
let adapter = bluetooth.getAdapter();
await adapter.startDiscovery();

6. 通知权限

NOTIFICATION_CONTROLLER(通知控制)

json5
{
  "name": "ohos.permission.NOTIFICATION_CONTROLLER",
  "reason": "$string:notification_reason"
}
// 等级:user_granted
// 用途:读取/发送通知
typescript
import { notification } from '@kit.ArkNotification';

// 发送通知
async function sendNotification() {
  let notificationInfo = {
    title: '通知标题',
    content: '通知内容',
    id: Date.now()
  };
  
  await notification.publish(notificationInfo);
}

7. 权限速查表

权限名称等级典型用途
INTERNETnormalHTTP 请求
ACCESS_NETWORK_STATEnormal网络状态查询
USE_BLUETOOTHnormal蓝牙通信
CAMERAuser_granted拍照录像
MICROPHONEuser_granted录音
LOCATIONuser_granted定位服务
READ_MEDIAuser_granted读取相册
WRITE_MEDIAuser_granted保存文件
NOTIFICATION_CONTROLLERuser_granted通知管理

8. 面试高频问题

Q1: 哪些权限是 normal 等级?

:INTERNET、ACCESS_NETWORK_STATE、USE_BLUETOOTH、VIBRATE 等低风险权限。

Q2: 相机权限申请后如何使用?

:申请成功后,使用 @kit.ArkCamera 模块的 API 进行拍照、录像或预览操作。

Q3: 位置权限的 when 配置有什么影响?

inuse 表示仅应用使用时可获取位置;always 表示后台也可获取位置(需特殊审批)。


面试提示:熟悉常用权限的名称、等级和使用方式是基础要求。