[经验分享] OpenHarmony 4.0 Release 应用自启动 原创

onefan 显示全部楼层 发表于 昨天 17:17

OpenHarmony 4.0 Release 应用自启动

需求场景:部分场景,三方应用、定制应用或系统应用需要在开机时就自启动指定应用,比如某些货物售卖机、售票窗口、检票口、打卡机等。

提供方案:

方案一:因开机动画及锁屏功能正常时,都是先显示桌面(launcher),故提供在launcher 创建窗口时,将某指定应用(非服务类型)拉起。

代码解析:

laucnher的窗口创建在工具类WindowManager.ts中, 我们可以将拉起操作放到createWindow或createRecentWindow中,以下是将拉起动作放到createRecentWindow中的示例: 1.为实现产品定制控制我们新增了DeviceUtil.isXxxProduct()来管控某个特定的产品, 2.为配合某xxx应用是否开机被拉起,特此新增persist.autostart.xxx属性来配合管控。 3.为配合网络场景,此外还添加了监听网络状态,以达到网络连接成功时再拉起指定应用。

<pre class="prettyprint linenums prettyprinted"><ol class="linenums"><li class="L0"><p><code><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="typ">DeviceUtil</span><span class="pun">.</span><span class="pln">isXxxProduct</span><span class="pun">())</span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L1" data-node-id="20250114171534-d4dhtam"><p><code><span class="pln"> </span><span class="typ">Log</span><span class="pun">.</span><span class="pln">showInfo</span><span class="pun">(</span><span class="pln">TAG</span><span class="pun">,</span><span class="pln"> </span><span class="str">'OneFan isXxxProduct'</span><span class="pun">);</span></code></p></li><li class="L2"><p><code><span class="pln"> </span><span class="com">// 先使用register接口注册订阅事件</span></code></p></li><li class="L3" data-node-id="20250114171534-yzthdyj"><p><code><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">netCon</span><span class="pun">.</span><span class="kwd">register</span><span class="pun">((</span><span class="pln">error</span><span class="pun">:</span><span class="pln"> </span><span class="typ">BusinessError</span><span class="pun">)</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L4"><p><code><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"OneFan"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">error</span><span class="pun">));</span></code></p></li><li class="L5" data-node-id="20250114171534-0g3d5aj"><p><code><span class="pln"> </span><span class="pun">});</span></code></p></li><li class="L6"><p><code><span class="pln"> </span><span class="com">// 订阅网络可用事件。调用register后,才能接收到此事件通知</span></code></p></li><li class="L7" data-node-id="20250114171534-kqmo925"><p><code><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">netCon</span><span class="pun">.</span><span class="pln">on</span><span class="pun">(</span><span class="str">'netAvailable'</span><span class="pun">,</span><span class="pln"> </span><span class="pun">(</span><span class="pln">data</span><span class="pun">:</span><span class="pln"> connection</span><span class="pun">.</span><span class="typ">NetHandle</span><span class="pun">)</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L8"><p><code><span class="pln"> console</span><span class="pun">.</span><span class="pln">info</span><span class="pun">(</span><span class="str">"OneFan Succeeded to get data: "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">data</span><span class="pun">));</span></code></p></li><li class="L9" data-node-id="20250114171534-9iihbyd"><p><code><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">systemparameter</span><span class="pun">.</span><span class="pln">getSync</span><span class="pun">(</span><span class="str">'persist.autostart.xxx'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'0'</span><span class="pun">)</span><span class="pln"> </span><span class="pun">===</span><span class="pln"> </span><span class="str">'1'</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L0"><p><code><span class="pln"> setTimeout</span><span class="pun">(()</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L1" data-node-id="20250114171534-sf4tsg6"><p><code><span class="pln"> </span><span class="kwd">let</span><span class="pln"> want </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L2"><p><code><span class="pln"> </span><span class="str">"deviceId"</span><span class="pun">:</span><span class="pln"> </span><span class="str">""</span><span class="pun">,</span></code></p></li><li class="L3" data-node-id="20250114171534-aeg9z0l"><p><code><span class="pln"> </span><span class="str">"bundleName"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"com.xx.xxx"</span><span class="pun">,</span></code></p></li><li class="L4"><p><code><span class="pln"> </span><span class="str">"abilityName"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"EntryAbility"</span></code></p></li><li class="L5" data-node-id="20250114171534-h17cxlr"><p><code><span class="pln"> </span><span class="pun">};</span></code></p></li><li class="L6"><p><code><span class="pln"> globalThis</span><span class="pun">.</span><span class="pln">desktopContext</span><span class="pun">.</span><span class="pln">startAbility</span><span class="pun">(</span><span class="pln">want</span><span class="pun">).</span><span class="kwd">then</span><span class="pun">(()</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L7" data-node-id="20250114171534-0slf4td"><p><code><span class="pln"> </span><span class="typ">Log</span><span class="pun">.</span><span class="pln">showInfo</span><span class="pun">(</span><span class="pln">TAG</span><span class="pun">,</span><span class="pln"> </span><span class="str">'OneFan startAbility success'</span><span class="pun">);</span></code></p></li><li class="L8"><p><code><span class="pln"> </span><span class="com">// 使用unregister接口取消订阅</span></code></p></li><li class="L9" data-node-id="20250114171534-73ugmdx"><p><code><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">netCon</span><span class="pun">.</span><span class="pln">unregister</span><span class="pun">((</span><span class="pln">error</span><span class="pun">:</span><span class="pln"> </span><span class="typ">BusinessError</span><span class="pun">)</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L0"><p><code><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"OneFan"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">error</span><span class="pun">));</span></code></p></li><li class="L1" data-node-id="20250114171534-bhk95cr"><p><code><span class="pln"> </span><span class="pun">});</span></code></p></li><li class="L2"><p><code><span class="pln"> </span><span class="pun">}).</span><span class="kwd">catch</span><span class="pun">((</span><span class="pln">error</span><span class="pun">)</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L3" data-node-id="20250114171534-lh7btli"><p><code><span class="pln"> </span><span class="typ">Log</span><span class="pun">.</span><span class="pln">showInfo</span><span class="pun">(</span><span class="pln">TAG</span><span class="pun">,</span><span class="pln"> </span><span class="str">'OneFan startAbility failed'</span><span class="pun">);</span></code></p></li><li class="L4"><p><code><span class="pln"> </span><span class="pun">})</span></code></p></li><li class="L5" data-node-id="20250114171534-oocci4m"><p><code><span class="pln"> </span><span class="pun">},</span><span class="pln"> </span><span class="lit">500</span><span class="pun">)</span></code></p></li><li class="L6"><p><code><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span></code></p></li><li class="L7" data-node-id="20250114171534-i3scpsm"><p><code><span class="pln"> </span><span class="typ">Log</span><span class="pun">.</span><span class="pln">showInfo</span><span class="pun">(</span><span class="pln">TAG</span><span class="pun">,</span><span class="pln"> </span><span class="str">'OneFan persist.autostart.xxx err'</span><span class="pun">);</span></code></p></li><li class="L8"><p><code><span class="pln"> </span><span class="pun">}</span></code></p></li><li class="L9" data-node-id="20250114171534-tkd34d4"><p><code><span class="pln"> </span><span class="pun">});</span></code></p></li><li class="L0"><p><code><span class="pln"> </span><span class="pun">}</span></code></p></li></ol></pre>

默认情况下,我们不会配置persist.autostart.xxx属性,故开启不会自启动某个指定应用,只有在persist.autostart.xxx 为 1时且网络OK时才会启动某应用。

在4.1R及之后的版本实现开机自启动则建议直接使用AutoStartupCallback.onAutoStartupOn接口即可。可参考https://docs.openharmony.cn/pages/v4.1/zh-cn/application-dev/reference/apis-ability-kit/js-apis-inner-application-autoStartupCallback-sys.md

而相对于服务的自启动配置来说,就相对比较容易了,预安装配置到install_list.json,自启动配置(应用特权配置) 到install_list_capability.json,具体可参考docs.openharmony.cn/pages/v4.0/zh-cn/device-dev/subsystems/subsys-app-privilege-config-guide.md/

©著作权归作者所有,转载或内容合作请联系作者

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部