找回密码
 立即注册
首页 业界区 业界 Electron 客户端开机自启动

Electron 客户端开机自启动

岳娅纯 5 天前
app.setLoginItemSettings 与 auto-launch 对比分析

一、稳定性对比

1. app.setLoginItemSettings


  • 优点:作为Electron官方API,有官方维护和支持
  • 缺点

    • 在某些Windows版本上存在已知问题
    • 部分Windows 10/11更新后可能失效
    • 在macOS权限更严格的版本上可能需要额外授权
    • 不支持Linux

2. auto-launch


  • 优点

    • 针对各平台做了特殊适配(Windows用注册表,macOS用Launch Services,Linux用.desktop文件)
    • 对系统权限问题有更好的处理和反馈
    • 经过多年实践验证,在各种系统环境下更稳定

  • 缺点

    • 依赖第三方库,理论上有维护风险(但该库活跃度良好)

二、易用性对比

1. app.setLoginItemSettings
  1. // 设置自启动
  2. app.setLoginItemSettings({
  3.   openAtLogin: true,
  4.   openAsHidden: false
  5. })
  6. // 检查状态 - 没有Promise支持
  7. const status = app.getLoginItemSettings()
  8. console.log('是否自启动:', status.openAtLogin)
复制代码
2. auto-launch
  1. // 创建实例
  2. const autoLauncher = new AutoLaunch({
  3.   name: app.getName(),
  4.   path: app.getPath('exe')
  5. })
  6. // 检查状态 - 支持Promise
  7. const isEnabled = await autoLauncher.isEnabled()
  8. // 启用/禁用 - 链式调用友好
  9. autoLauncher.isEnabled()
  10.   .then(isEnabled => {
  11.     if (!isEnabled) return autoLauncher.enable()
  12.   })
  13.   .then(() => console.log('自启动已启用'))
  14.   .catch(err => console.error('操作失败', err))
复制代码
三、功能对比

功能app.setLoginItemSettingsauto-launchWindows支持✅✅macOS支持✅✅Linux支持❌✅Promise支持❌✅错误处理有限完善状态检查简单完善隐藏启动✅ (macOS 已弃用)✅维护状态官方维护社区活跃四、实际使用

1. auto-launch.ts 文件
  1. pnpm install auto-launch
复制代码
  1. import AutoLaunch from 'auto-launch'
  2. import { app } from 'electron'
  3. import log from 'electron-log/main'
  4. /**
  5. * 设置应用开机自启动
  6. * @param enable 是否启用自启动,默认为true
  7. */
  8. export function setupAutoLaunch(enable: boolean = true): void {
  9.   const autoLauncher = new AutoLaunch({
  10.     name: app.getName(),
  11.     path: process.execPath,
  12.   })
  13.   if (enable) {
  14.     autoLauncher.isEnabled()
  15.       .then((isEnabled) => {
  16.         if (!isEnabled) {
  17.           autoLauncher.enable()
  18.             .then(() => log.info('已启用自启动'))
  19.             .catch(err => log.error('启用自启动失败:', err))
  20.         }
  21.         else {
  22.           log.info('自启动已经启用')
  23.         }
  24.       })
  25.       .catch(err => log.error('检查自启动状态失败:', err))
  26.   }
  27.   else {
  28.     autoLauncher.isEnabled()
  29.       .then((isEnabled) => {
  30.         if (isEnabled) {
  31.           autoLauncher.disable()
  32.             .then(() => log.info('已禁用自启动'))
  33.             .catch(err => log.error('禁用自启动失败:', err))
  34.         }
  35.         else {
  36.           log.info('自启动已经禁用')
  37.         }
  38.       })
  39.       .catch(err => log.error('检查自启动状态失败:', err))
  40.   }
  41. }
复制代码
2. 在 main/index.ts 文件中使用
  1. import { setupAutoLaunch } from './utils/auto-launch'
  2. async function electronAppInit() {
  3.   log.info('主进程已启动')
  4.   
  5.   // 设置应用自启动
  6.   setupAutoLaunch(true)
  7.   app.on('window-all-closed', () => {
  8.     if (process.platform !== PLATFORM.DARWIN) {
  9.       log.info('主进程已关闭')
  10.       app.quit()
  11.     }
  12.   })
  13. }
复制代码
3. 体验

实际开发中,auto-launch 提供了更一致的开发体验:

  • 错误处理更清晰:当遇到权限问题时,auto-launch 提供明确的错误信息,而 app.setLoginItemSettings 可能静默失败
  • Windows兼容性更好:Windows系统更新频繁,auto-launch 通过直接操作注册表提供了更稳定的行为
  • 跨平台一致性:如果你的应用需要支持Linux,只能选择 auto-launch
  • 代码组织更清晰:Promise支持让异步操作处理更优雅
五、结论

综合稳定性和易用性考虑,推荐使用 auto-launch,特别是:

  • 如果你的应用需要支持Linux
  • 如果你重视更好的错误处理和用户反馈
  • 如果你的应用在Windows平台有较多用户(Windows更新可能影响原生API)
app.setLoginItemSettings 更适合简单场景,或者你特别关注减少依赖项的情况。但整体而言,auto-launch 提供了更可靠和一致的开发体验。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册