开发一个App,桌面有三个入口,分别显示不同的内容

1、入口图标不一样,桌面名称分别为:TEXT、IMAGE、VIDEO。

2、分别进入不同的页面(分别用Text组件、Image组件、Video组件作为首页)

3、从桌面启动时,Text组件、Image组件、Video组件,分别使用沙箱内的txt、jpg、mp4文件进行初始化

HarmonyOS
2024-05-22 23:01:12
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
友人FA

第一个功能可以在module.json5配置中心中配置多个abilities,一个ability设置一个显示图标和一个图标名称。

第二个功能可以设置多个entryability页面,然后在module.json5中一个ability关联一个entryability页面,然后再从entryability页面中的loadContent跳转到想要跳转组件页面。

第三个功能沙箱内的txt文件需要通过r()引用element下的string.json文件内的内容,Image组件类似于txt文件,引用的是media下的图片文件,Video组件通过r()引用element下的string.json文件内的内容,Image组件类似于txt文件,引用的是media下的图片文件,Video组件通过r()引用element下的string.json文件内的内容,Image组件类似于txt文件,引用的是media下的图片文件,Video组件通过rawfile()引用rawfile下的video文件。

具体代码展示

Entryability.ets: 
import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 
import hilog from '@ohos.hilog'; 
import UIAbility from '@ohos.app.ability.UIAbility'; 
import Want from '@ohos.app.ability.Want'; 
import window from '@ohos.window'; 
  
export default class EntryAbility extends UIAbility { 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 
  } 
  
  onDestroy(): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 
  } 
  
  onWindowStageCreate(windowStage: window.WindowStage): void { 
    // Main window is created, set main page for this ability 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
  
    windowStage.loadContent('pages/Index', (err, data) => { 
      if (err.code) { 
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 
        return; 
      } 
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 
    }); 
  } 
  
  onWindowStageDestroy(): void { 
    // Main window is destroyed, release UI related resources 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 
  } 
  
  onForeground(): void { 
    // Ability has brought to foreground 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); 
  } 
  
  onBackground(): void { 
    // Ability has back to background 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 
  } 
} 
EntryAbility2.ets: 
import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 
import hilog from '@ohos.hilog'; 
import UIAbility from '@ohos.app.ability.UIAbility'; 
import Want from '@ohos.app.ability.Want'; 
import window from '@ohos.window'; 
  
export default class EntryAbility extends UIAbility { 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 
  } 
  
  onDestroy(): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 
  } 
  
  onWindowStageCreate(windowStage: window.WindowStage): void { 
    // Main window is created, set main page for this ability 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
  
    windowStage.loadContent('pages/Index2', (err, data) => { 
      if (err.code) { 
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 
        return; 
      } 
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 
    }); 
  } 
  
  onWindowStageDestroy(): void { 
    // Main window is destroyed, release UI related resources 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 
  } 
  
  onForeground(): void { 
    // Ability has brought to foreground 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); 
  } 
  
  onBackground(): void { 
    // Ability has back to background 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 
  } 
} 
EntryAbility3.ets: 
import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 
import hilog from '@ohos.hilog'; 
import UIAbility from '@ohos.app.ability.UIAbility'; 
import Want from '@ohos.app.ability.Want'; 
import window from '@ohos.window'; 
  
export default class EntryAbility extends UIAbility { 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 
  } 
  
  onDestroy(): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 
  } 
  
  onWindowStageCreate(windowStage: window.WindowStage): void { 
    // Main window is created, set main page for this ability 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
  
    windowStage.loadContent('pages/Index3', (err, data) => { 
      if (err.code) { 
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 
        return; 
      } 
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 
    }); 
  } 
  
  onWindowStageDestroy(): void { 
    // Main window is destroyed, release UI related resources 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 
  } 
  
  onForeground(): void { 
    // Ability has brought to foreground 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); 
  } 
  
  onBackground(): void { 
    // Ability has back to background 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 
  } 
} 
Index.ets: 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
  
  build() { 
    Row() { 
      Column() { 
        Text($r('app.string.EntryAbility_TEXT')) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
Index2: 
@Entry 
@Component 
struct Index2 { 
  @State message: string = 'Hello World'; 
  
  build() { 
    Row() { 
      Column() { 
       Image($r('app.media.R')) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
Index3: 
@Entry 
@Component 
struct Index3 { 
  @State message: string = 'Hello World'; 
  private controller:VideoController | undefined; 
  private previewUris: Resource = $r ('app.media.R'); 
  private innerResource: Resource = $rawfile('R1.mp4'); 
  
  build() { 
    Row() { 
      Column() { 
        Video({ 
          src: this.innerResource, 
          previewUri: this.previewUris, 
          controller: this.controller 
        }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
module.json: 
{ 
  "module": { 
    "name": "entry_test", 
    "type": "feature", 
    "description": "$string:module_test_desc", 
    "mainElement": "TestAbility", 
    "deviceTypes": [ 
      "phone", 
      "tablet", 
      "2in1" 
    ], 
    "deliveryWithInstall": true, 
    "installationFree": false, 
    "pages": "$profile:test_pages", 
    "requestPermissions": [ 
      { 
        "name": "ohos.permission.WRITE_CALENDAR" 
      }, 
      { 
        "name": "ohos.permission.READ_CALENDAR" 
      } 
    ], 
    "abilities": [ 
      { 
        "name": "TestAbility", 
        "srcEntry": "./ets/testability/TestAbility.ets", 
        "description": "$string:TestAbility_desc", 
        "icon": "$media:icon", 
        "label": "$string:TestAbility_label", 
        "exported": true, 
        "startWindowIcon": "$media:icon", 
        "startWindowBackground": "$color:start_window_background", 
        "skills": [ 
          { 
            "actions": [ 
              "action.system.home" 
            ], 
            "entities": [ 
              "entity.system.home" 
            ] 
          } 
        ] 
      } 
    ] 
  } 
}

适配的版本信息

IDE:DevEco Studio 4.1.3.319

SDK:HarmoneyOS 4.0.0.41

分享
微博
QQ
微信
回复
2024-05-23 17:19:06
相关问题
一个app不同hap如何共享状态?
233浏览 • 1回复 待解决
三个设备如何同时实现多端协同?
5430浏览 • 1回复 待解决
如何知道一个组件显示和隐藏
605浏览 • 1回复 待解决
长按滑动显示不同内容
857浏览 • 1回复 待解决
HarmonyOS Text 超出限制显示一个更多
22浏览 • 1回复 待解决