当前位置: 首页 > news >正文

淘客做的网站属于什么类型软件类专业有哪些

淘客做的网站属于什么类型,软件类专业有哪些,江门搜狗网站推广优化,wordpress更换网址后台怎么进【HarmonyOS NEXT】鸿蒙应用使用后台任务之长时任务,解决屏幕录制音乐播放等操作不被挂起 一、前言 1.后台是什么? 了解后台任务和长时任务前,我们需要先明白鸿蒙的后台特性:所谓的后台,指的是设备返回主界面、锁屏、…

【HarmonyOS NEXT】鸿蒙应用使用后台任务之长时任务,解决屏幕录制音乐播放等操作不被挂起

一、前言

1.后台是什么?
了解后台任务和长时任务前,我们需要先明白鸿蒙的后台特性:所谓的后台,指的是设备返回主界面、锁屏、应用切换等操作会使应用退至后台这个状态。

2.鸿蒙系统为什么这么做?
当应用退至后台后,如果继续活动,可能会造成设备耗电快、用户界面卡顿等现象。鸿蒙系统为了降低设备耗电速度、保障用户使用流畅度,系统会对退至后台的应用进行管控,包括进程挂起和进程终止。

3.会有什么问题?
当系统将应用挂起后,应用进程无法使用软件资源(如公共事件、定时器等)和硬件资源(CPU、网络、GPS、蓝牙等)。

综上所述,所以才会有标题存在的问题和对应的解决方案。
当我们应用正在使用蓝牙扫描 或者 音乐播放 或者 屏幕录制等类似的操作时,只要应用退到了后台超过三秒,就会被系统挂起,强制暂停。影响我们的逻辑业务。
所以这种情况下,鸿蒙提供了后台任务来解决。

二、后台任务是什么

后台任务是鸿蒙系统提供给有在后台,做业务操作不想被挂起需求的应用,提供的一套解决方案。

根据应用业务类型不同,也分为不同的后台任务:
在这里插入图片描述
根据我们的常规使用场景,例如屏幕录制举例,就需要使用长时任务来解决应用被挂起的问题。

三、长时任务的使用:

1.首先我们需要根据自己的业务类型,选择对应的长时任务类型:
在这里插入图片描述
我们以屏幕录制举例,选择AUDIO_RECORDING

2.在module.json5配置后台任务权限和长时任务能力类型:

   "abilities": [{"backgroundModes": ["audioRecording"],}
      // 申请长时任务{"name": "ohos.permission.KEEP_BACKGROUND_RUNNING","reason": "$string:reason","usedScene": {"abilities": ["EntryAbility"],"when": "always"}},

3.在录屏开启前,调用开启长时任务,退出录屏后取消长时任务。【开启和取消的两个调用时机需要注意,相当于长时任务的生命周期,是包裹住整个后台业务的生命周期。】

开启长时任务

import { backgroundTaskManager } from '@kit.BackgroundTasksKit';
import { wantAgent, WantAgent } from '@kit.AbilityKit';/*** 开启长时任务*/startContinuousTask() {let wantAgentInfo: wantAgent.WantAgentInfo = {// 点击通知后,将要执行的动作列表// 添加需要被拉起应用的bundleName和abilityNamewants: [{bundleName: "com.test.basedemo",abilityName: "EntryAbility"}],// 指定点击通知栏消息后的动作是拉起abilityactionType: wantAgent.OperationType.START_ABILITY,// 使用者自定义的一个私有值requestCode: 0,// 点击通知后,动作执行属性actionFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]};try {// 通过wantAgent模块下getWantAgent方法获取WantAgent对象wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj: WantAgent) => {try {backgroundTaskManager.startBackgroundRunning(getContext(),backgroundTaskManager.BackgroundMode.AUDIO_RECORDING, wantAgentObj, (error: BusinessError)=>{if (error) {console.error(this.TAG,`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);} else {console.info(this.TAG,"Operation startBackgroundRunning succeeded");promptAction.showToast({message: "开启长时任务成功!"});// // 此处执行具体的长时任务逻辑,如录音,录制等。// this.startRecording();}})} catch (error) {console.error(this.TAG,`Operation startBackgroundRunning failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);}});} catch (error) {console.error(this.TAG, `Failed to Operation getWantAgent. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);}}

关闭长时任务

  /*** 暂停长时任务*/stopContinuousTask() {backgroundTaskManager.stopBackgroundRunning(getContext()).then(() => {console.info(this.TAG, `Succeeded in operationing stopBackgroundRunning.`);promptAction.showToast({message: "取消长时任务!"});}).catch((err: BusinessError) => {console.error(this.TAG, `Failed to operation stopBackgroundRunning. Code is ${err.code}, message is ${err.message}`);});}

源码示例:

在这里插入图片描述

module.json5

   "abilities": [{"backgroundModes": ["audioRecording"],}
      // 申请长时任务{"name": "ohos.permission.KEEP_BACKGROUND_RUNNING","reason": "$string:reason","usedScene": {"abilities": ["EntryAbility"],"when": "always"}},// 申请麦克风{"name": "ohos.permission.MICROPHONE","reason": "$string:reason","usedScene": {"abilities": ["EntryAbility"],"when": "always"}},

BackTaskTestPage.ets


import media from '@ohos.multimedia.media';
import { backgroundTaskManager } from '@kit.BackgroundTasksKit';
import { abilityAccessCtrl, common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { wantAgent, WantAgent } from '@kit.AbilityKit';
import { fileIo as fs } from '@kit.CoreFileKit';
import { promptAction } from '@kit.ArkUI';

struct BackTaskTestPage {private TAG: string = "BackTaskTestPage";// 录屏沙箱文件private mFile: fs.File | null = null;aboutToAppear(): void {// 为了录屏可以采集麦克风,需要申请麦克风权限const atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();try {atManager.requestPermissionsFromUser(getContext(), ["ohos.permission.MICROPHONE"]).then((data) => {if (data.authResults[0] === 0) {} else {console.log(this.TAG, "user rejected")}}).catch((err: BusinessError) => {console.log(this.TAG, "BusinessError err: " + JSON.stringify(err))})} catch (err) {console.log(this.TAG, "catch err: " + JSON.stringify(err))}// 创建录制视频的沙箱文件地址let context = getContext(this) as common.UIAbilityContext; // 获取设备A的UIAbilityContext信息let pathDir: string = context.filesDir; // /data/storage/el2/base/haps/entry/fileslet filePath: string = pathDir + '/testBG.mp4';// 若文件不存在,则创建文件。let fileTarget = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);console.info(this.TAG, "file done: " + JSON.stringify(fileTarget.fd));this.mFile = fileTarget;}/*** 开启长时任务*/startContinuousTask() {let wantAgentInfo: wantAgent.WantAgentInfo = {// 点击通知后,将要执行的动作列表// 添加需要被拉起应用的bundleName和abilityNamewants: [{bundleName: "com.test.basedemo",abilityName: "EntryAbility"}],// 指定点击通知栏消息后的动作是拉起abilityactionType: wantAgent.OperationType.START_ABILITY,// 使用者自定义的一个私有值requestCode: 0,// 点击通知后,动作执行属性actionFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]};try {// 通过wantAgent模块下getWantAgent方法获取WantAgent对象wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj: WantAgent) => {try {backgroundTaskManager.startBackgroundRunning(getContext(),backgroundTaskManager.BackgroundMode.AUDIO_RECORDING, wantAgentObj, (error: BusinessError)=>{if (error) {console.error(this.TAG,`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);} else {console.info(this.TAG,"Operation startBackgroundRunning succeeded");promptAction.showToast({message: "开启长时任务成功!"});// // 此处执行具体的长时任务逻辑,如录音,录制等。// this.startRecording();}})} catch (error) {console.error(this.TAG,`Operation startBackgroundRunning failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);}});} catch (error) {console.error(this.TAG, `Failed to Operation getWantAgent. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);}}/*** 暂停长时任务*/stopContinuousTask() {backgroundTaskManager.stopBackgroundRunning(getContext()).then(() => {console.info(this.TAG, `Succeeded in operationing stopBackgroundRunning.`);promptAction.showToast({message: "取消长时任务!"});}).catch((err: BusinessError) => {console.error(this.TAG, `Failed to operation stopBackgroundRunning. Code is ${err.code}, message is ${err.message}`);});}build() {Row() {Column() {Button() {Text('申请长时任务').fontSize(25).fontWeight(FontWeight.Bold)}.type(ButtonType.Capsule).margin({ top: 10 }).backgroundColor('#0D9FFB').width(250).height(40).onClick(() => {// 通过按钮申请长时任务this.startContinuousTask();})Button() {Text('取消长时任务').fontSize(25).fontWeight(FontWeight.Bold)}.type(ButtonType.Capsule).margin({ top: 10 }).backgroundColor('#0D9FFB').width(250).height(40).onClick(() => {// 通过按钮取消长时任务this.stopContinuousTask();})Button() {Text('开始录制').fontSize(25).fontWeight(FontWeight.Bold)}.type(ButtonType.Capsule).margin({ top: 10 }).backgroundColor('#0D9FFB').width(250).height(40).onClick(async () => {this.startRecording();})Button() {Text('暂停录制').fontSize(25).fontWeight(FontWeight.Bold)}.type(ButtonType.Capsule).margin({ top: 10 }).backgroundColor('#0D9FFB').width(250).height(40).onClick(() => {this.stopRecording();})}.width('100%')}.height('100%')}private screenCapture?: media.AVScreenCaptureRecorder;// 调用startRecording方法可以开始一次录屏存文件的流程,结束录屏可以通过点击录屏胶囊停止按钮进行操作。public async startRecording() {this.screenCapture = await media.createAVScreenCaptureRecorder();console.info(this.TAG,"startRecording screenCapture on done" );try {let avCaptureConfig: media.AVScreenCaptureRecordConfig = {fd: this.mFile?.fd ?? 0, // 文件需要先有调用者创建,赋予写权限,将文件fd传给此参数}await this.screenCapture?.init(avCaptureConfig); // avCaptureConfig captureConfigconsole.info(this.TAG,"startRecording screenCapture init done" );} catch (err) {console.info(this.TAG,"startRecording init err: " + JSON.stringify(err) );}await this.screenCapture?.startRecording();console.info(this.TAG,"startRecording screenCapture startRecording" );}// 可以主动调用stopRecording方法来停止录屏。public async stopRecording() {if (this.screenCapture == undefined) {// Errorreturn;}await this.screenCapture?.stopRecording();// 调用release()方法销毁实例,释放资源。await this.screenCapture?.release();// 最后需要关闭创建的录屏文件fd, fs.close(fd);fs.closeSync(this.mFile);}
}
http://www.yayakq.cn/news/704867/

相关文章:

  • 沈阳网站制作 600元佛山网站建设费用
  • 重庆 网站 建设wordpress文章推送邮箱
  • 做外贸企业网站要注意哪些中山网站建设价位
  • wordpress卖邀请码链接南昌seo推广外包
  • wordpress站点结构做外贸没网站可以吗
  • 各种网站开发语言的优缺点微信自己开发小程序
  • 如何用手机做钓鱼网站威海 网站开发
  • 织梦m网站伪静态做网站和优化共多少钱
  • 济南网站建设公司有哪些国外搜索引擎排名百鸣
  • 湛江网站建设电话如何做自己的淘宝网站
  • 网站开发 工具wordpress 弹框
  • 搭建个人网站的两种方法网站如何自己做优化
  • 众网站阿里云oss可以做网站
  • 哪种语言做网站最合适网上商城开发费用
  • 有后台的网站中国联通网站备案管理系统
  • 校园网站建设说明书网页设计公司网站设计
  • 营销网站建设是什么意思如何进行公司网站的建设
  • 网站建设工作年报网站更换程序
  • 网站备案字号免费推广的途径与原因
  • 网站建设工作情况报告cms网站如何修改
  • 网站模版怎么做代做百度首页排名
  • 好建网站河南省住房和建设厅网站
  • 网站建设部工作职能网站维护和推广方案
  • dw网站结构图怎么做哪个网站专做滨水景观
  • 开发网站做什么软件开发工程师级别
  • 网站平台需要做无形资产吗 怎么做6网络优化工程师工资一般多少
  • 自己做视频直播网站3d展示网站源码
  • 企业营销型网站类型营销型网站建
  • 网站建设怎样wordpress 个人 主题
  • 东莞建外贸企业网站怎样用jsp做网站