diff --git a/ArkUI/orientationDevelopment/README.md b/ArkUI/orientationDevelopment/README.md new file mode 100644 index 0000000000000000000000000000000000000000..f58c9803656078eec5924b8d9cda3a1b59b0ae7f --- /dev/null +++ b/ArkUI/orientationDevelopment/README.md @@ -0,0 +1,44 @@ +# 多设备功能开发同源代码工程 + +## 介绍 + +本示例为多设备功能开发同源代码工程,包含最佳实践文档中包含的推荐使用方法的样例代码。工程本身不具备实际功能,开发者请直接阅读文档结合源码来理解多设备功能开发。 + +## 效果预览 + +不涉及。 + +## 工程目录 + +``` +entry/src/main/ets +│ ├──entryability +│ │ └──entryability // 应用入口类 +│ ├──entrybackupability +│ │ └──EntryBackupAbility.ets // 应用备份能力类 +│ └──pages +│ └──Index.ets // 首页 +└────entry/src/main/resources +``` + +## 具体实现 + +不涉及。 + +## 相关权限 + +不涉及。 + +## 依赖 + +不涉及。 + +## 约束与限制 + +1.本示例仅支持标准系统上运行,支持设备:华为手机。 + +2.HarmonyOS系统:HarmonyOS 5.0.5 Release及以上。 + +3.DevEco Studio版本:DevEco Studio 5.0.5 Release及以上。 + +4.HarmonyOS SDK版本:HarmonyOS 5.0.5 Release SDK及以上。 \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entryability/EntryAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entryability/EntryAbility.ets deleted file mode 100644 index 508880af8c33aa838016d1cd4b2c68be2f447540..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entryability/EntryAbility.ets +++ /dev/null @@ -1,44 +0,0 @@ -import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; -import { hilog } from '@kit.PerformanceAnalysisKit'; -import { window } from '@kit.ArkUI'; - -const DOMAIN = 0x0000; - -export default class EntryAbility extends UIAbility { - onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { - this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); - } - - onDestroy(): void { - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); - } - - onWindowStageCreate(windowStage: window.WindowStage): void { - // Main window is created, set main page for this ability - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); - - windowStage.loadContent('pages/Index', (err) => { - if (err.code) { - hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); - return; - } - hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); - }); - } - - onWindowStageDestroy(): void { - // Main window is destroyed, release UI related resources - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); - } - - onForeground(): void { - // Ability has brought to foreground - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); - } - - onBackground(): void { - // Ability has back to background - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); - } -} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entrybackupability/EntryBackupAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entrybackupability/EntryBackupAbility.ets deleted file mode 100644 index 8e4de99282050bad799ac892eb85ac5449364a51..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entrybackupability/EntryBackupAbility.ets +++ /dev/null @@ -1,16 +0,0 @@ -import { hilog } from '@kit.PerformanceAnalysisKit'; -import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; - -const DOMAIN = 0x0000; - -export default class EntryBackupAbility extends BackupExtensionAbility { - async onBackup() { - hilog.info(DOMAIN, 'testTag', 'onBackup ok'); - await Promise.resolve(); - } - - async onRestore(bundleVersion: BundleVersion) { - hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); - await Promise.resolve(); - } -} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/pages/Index.ets b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/pages/Index.ets deleted file mode 100644 index 8e2d24ad42693fc877d51bb7820f0a9da68fa135..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/pages/Index.ets +++ /dev/null @@ -1,23 +0,0 @@ -@Entry -@Component -struct Index { - @State message: string = 'Hello World'; - - build() { - RelativeContainer() { - Text(this.message) - .id('HelloWorld') - .fontSize($r('app.float.page_text_font_size')) - .fontWeight(FontWeight.Bold) - .alignRules({ - center: { anchor: '__container__', align: VerticalAlign.Center }, - middle: { anchor: '__container__', align: HorizontalAlign.Center } - }) - .onClick(() => { - this.message = 'Welcome'; - }) - } - .height('100%') - .width('100%') - } -} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/module.json5 b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/module.json5 deleted file mode 100644 index 01b6011d32a16efa9066ff41154ba5b688b2efa3..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/module.json5 +++ /dev/null @@ -1,64 +0,0 @@ -// [Start module] -{ - "module": { - // [StartExclude module] - "name": "entry", - "type": "entry", - "description": "$string:module_desc", - "mainElement": "EntryAbility", - "deviceTypes": [ - "phone", - "tablet", - "2in1", - "wearable" - ], - "deliveryWithInstall": true, - "installationFree": false, - "pages": "$profile:main_pages", - // [EndExclude module] - "abilities": [ - { - "name": "EntryAbility", - // [StartExclude module] - "srcEntry": "./ets/entryability/EntryAbility.ets", - "description": "$string:EntryAbility_desc", - "icon": "$media:layered_image", - "label": "$string:EntryAbility_label", - "startWindowIcon": "$media:startIcon", - // [EndExclude module] - "orientation": "auto_rotation", - // [StartExclude module] - "startWindowBackground": "$color:start_window_background", - "exported": true, - "skills": [ - { - "entities": [ - "entity.system.home" - ], - "actions": [ - "action.system.home" - ] - } - ], - // [EndExclude module] - } - ], - // [StartExclude module] - "extensionAbilities": [ - { - "name": "EntryBackupAbility", - "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", - "type": "backup", - "exported": false, - "metadata": [ - { - "name": "ohos.extension.backup", - "resource": "$profile:backup_config" - } - ], - } - ], - // [EndExclude module] - } -} -// [End module] \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entryability/EntryAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entryability/EntryAbility.ets deleted file mode 100644 index 508880af8c33aa838016d1cd4b2c68be2f447540..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entryability/EntryAbility.ets +++ /dev/null @@ -1,44 +0,0 @@ -import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; -import { hilog } from '@kit.PerformanceAnalysisKit'; -import { window } from '@kit.ArkUI'; - -const DOMAIN = 0x0000; - -export default class EntryAbility extends UIAbility { - onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { - this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); - } - - onDestroy(): void { - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); - } - - onWindowStageCreate(windowStage: window.WindowStage): void { - // Main window is created, set main page for this ability - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); - - windowStage.loadContent('pages/Index', (err) => { - if (err.code) { - hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); - return; - } - hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); - }); - } - - onWindowStageDestroy(): void { - // Main window is destroyed, release UI related resources - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); - } - - onForeground(): void { - // Ability has brought to foreground - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); - } - - onBackground(): void { - // Ability has back to background - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); - } -} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entrybackupability/EntryBackupAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entrybackupability/EntryBackupAbility.ets deleted file mode 100644 index 8e4de99282050bad799ac892eb85ac5449364a51..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entrybackupability/EntryBackupAbility.ets +++ /dev/null @@ -1,16 +0,0 @@ -import { hilog } from '@kit.PerformanceAnalysisKit'; -import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; - -const DOMAIN = 0x0000; - -export default class EntryBackupAbility extends BackupExtensionAbility { - async onBackup() { - hilog.info(DOMAIN, 'testTag', 'onBackup ok'); - await Promise.resolve(); - } - - async onRestore(bundleVersion: BundleVersion) { - hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); - await Promise.resolve(); - } -} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/pages/Index.ets b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/pages/Index.ets deleted file mode 100644 index 8e2d24ad42693fc877d51bb7820f0a9da68fa135..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/pages/Index.ets +++ /dev/null @@ -1,23 +0,0 @@ -@Entry -@Component -struct Index { - @State message: string = 'Hello World'; - - build() { - RelativeContainer() { - Text(this.message) - .id('HelloWorld') - .fontSize($r('app.float.page_text_font_size')) - .fontWeight(FontWeight.Bold) - .alignRules({ - center: { anchor: '__container__', align: VerticalAlign.Center }, - middle: { anchor: '__container__', align: HorizontalAlign.Center } - }) - .onClick(() => { - this.message = 'Welcome'; - }) - } - .height('100%') - .width('100%') - } -} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/module.json5 b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/module.json5 deleted file mode 100644 index f45f9d029f91c61f81f22b479b62672587a1db69..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/module.json5 +++ /dev/null @@ -1,64 +0,0 @@ -// [Start module] -{ - "module": { - // [StartExclude module] - "name": "entry", - "type": "entry", - "description": "$string:module_desc", - "mainElement": "EntryAbility", - "deviceTypes": [ - "phone", - "tablet", - "2in1", - "wearable" - ], - "deliveryWithInstall": true, - "installationFree": false, - "pages": "$profile:main_pages", - // [EndExclude module] - "abilities": [ - { - "name": "EntryAbility", - // [StartExclude module] - "srcEntry": "./ets/entryability/EntryAbility.ets", - "description": "$string:EntryAbility_desc", - "icon": "$media:layered_image", - "label": "$string:EntryAbility_label", - "startWindowIcon": "$media:startIcon", - // [EndExclude module] - "preferMultiWindowOrientation": "landscape_auto", - // [StartExclude module] - "startWindowBackground": "$color:start_window_background", - "exported": true, - "skills": [ - { - "entities": [ - "entity.system.home" - ], - "actions": [ - "action.system.home" - ] - } - ], - // [EndExclude module] - } - ], - // [StartExclude module] - "extensionAbilities": [ - { - "name": "EntryBackupAbility", - "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", - "type": "backup", - "exported": false, - "metadata": [ - { - "name": "ohos.extension.backup", - "resource": "$profile:backup_config" - } - ], - } - ], - // [EndExclude module] - } -} -// [End module] \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entryability/EntryAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entryability/EntryAbility.ets deleted file mode 100644 index 508880af8c33aa838016d1cd4b2c68be2f447540..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entryability/EntryAbility.ets +++ /dev/null @@ -1,44 +0,0 @@ -import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; -import { hilog } from '@kit.PerformanceAnalysisKit'; -import { window } from '@kit.ArkUI'; - -const DOMAIN = 0x0000; - -export default class EntryAbility extends UIAbility { - onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { - this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); - } - - onDestroy(): void { - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); - } - - onWindowStageCreate(windowStage: window.WindowStage): void { - // Main window is created, set main page for this ability - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); - - windowStage.loadContent('pages/Index', (err) => { - if (err.code) { - hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); - return; - } - hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); - }); - } - - onWindowStageDestroy(): void { - // Main window is destroyed, release UI related resources - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); - } - - onForeground(): void { - // Ability has brought to foreground - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); - } - - onBackground(): void { - // Ability has back to background - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); - } -} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entrybackupability/EntryBackupAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entrybackupability/EntryBackupAbility.ets deleted file mode 100644 index 8e4de99282050bad799ac892eb85ac5449364a51..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entrybackupability/EntryBackupAbility.ets +++ /dev/null @@ -1,16 +0,0 @@ -import { hilog } from '@kit.PerformanceAnalysisKit'; -import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; - -const DOMAIN = 0x0000; - -export default class EntryBackupAbility extends BackupExtensionAbility { - async onBackup() { - hilog.info(DOMAIN, 'testTag', 'onBackup ok'); - await Promise.resolve(); - } - - async onRestore(bundleVersion: BundleVersion) { - hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); - await Promise.resolve(); - } -} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/pages/Index.ets b/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/pages/Index.ets deleted file mode 100644 index 8e2d24ad42693fc877d51bb7820f0a9da68fa135..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/pages/Index.ets +++ /dev/null @@ -1,23 +0,0 @@ -@Entry -@Component -struct Index { - @State message: string = 'Hello World'; - - build() { - RelativeContainer() { - Text(this.message) - .id('HelloWorld') - .fontSize($r('app.float.page_text_font_size')) - .fontWeight(FontWeight.Bold) - .alignRules({ - center: { anchor: '__container__', align: VerticalAlign.Center }, - middle: { anchor: '__container__', align: HorizontalAlign.Center } - }) - .onClick(() => { - this.message = 'Welcome'; - }) - } - .height('100%') - .width('100%') - } -} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configportrait/module.json5 b/ArkUI/orientationDevelopment/entry/src/main/configportrait/module.json5 deleted file mode 100644 index e53c330271e45c24bcf12f1209675100aa3a2ebc..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/configportrait/module.json5 +++ /dev/null @@ -1,64 +0,0 @@ -// [Start module] -{ - "module": { - // [StartExclude module] - "name": "entry", - "type": "entry", - "description": "$string:module_desc", - "mainElement": "EntryAbility", - "deviceTypes": [ - "phone", - "tablet", - "2in1", - "wearable" - ], - "deliveryWithInstall": true, - "installationFree": false, - "pages": "$profile:main_pages", - // [EndExclude module] - "abilities": [ - { - "name": "EntryAbility", - // [StartExclude module] - "srcEntry": "./ets/entryability/EntryAbility.ets", - "description": "$string:EntryAbility_desc", - "icon": "$media:layered_image", - "label": "$string:EntryAbility_label", - "startWindowIcon": "$media:startIcon", - // [EndExclude module] - "orientation": "portrait", - // [StartExclude module] - "startWindowBackground": "$color:start_window_background", - "exported": true, - "skills": [ - { - "entities": [ - "entity.system.home" - ], - "actions": [ - "action.system.home" - ] - } - ], - // [EndExclude module] - } - ], - // [StartExclude module] - "extensionAbilities": [ - { - "name": "EntryBackupAbility", - "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", - "type": "backup", - "exported": false, - "metadata": [ - { - "name": "ohos.extension.backup", - "resource": "$profile:backup_config" - } - ], - } - ], - // [EndExclude module] - } -} -// [End module] \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/Index.ets b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/Index.ets index 1c7a6e8aafb095c7e79d163cd5bf9ff1af6efdde..5c9b16d972f642af02cd37e30584c3301d8a495b 100644 --- a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/Index.ets +++ b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/Index.ets @@ -1,5 +1,6 @@ // [Start enable_nfc] import { nfcController } from '@kit.ConnectivityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; @Entry @Component diff --git a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/MediaQueryPage.ets b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/MediaQueryPage.ets deleted file mode 100644 index c71a990e05a7a705c53851282dd948e83fc9c398..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/MediaQueryPage.ets +++ /dev/null @@ -1,71 +0,0 @@ -import { mediaquery, window } from '@kit.ArkUI'; -import { common } from '@kit.AbilityKit'; - -// [Start Media] -@Entry -@Component -struct MediaQueryPage { - @State color: string = '#DB7093'; - @State text: string = 'Portrait'; - // The condition is true when the device is in landscape orientation - listener: mediaquery.MediaQueryListener = - this.getUIContext().getMediaQuery().matchMediaSync('(orientation: landscape)'); - // When the media query conditions are met, a callback is triggered - onPortrait(mediaQueryResult: mediaquery.MediaQueryResult) { - if (mediaQueryResult.matches as boolean) { - // If the device is in landscape orientation, change the corresponding page layout - this.color = '#FFD700'; - this.text = 'Landscape'; - } else { - this.color = '#DB7093'; - this.text = 'Portrait'; - } - } - aboutToAppear() { - // Bind the current application instance - // Bind a callback function - this.listener.on('change', (mediaQueryResult: mediaquery.MediaQueryResult) => { - this.onPortrait(mediaQueryResult) - }); - } - aboutToDisappear() { - // Unbind the callback function registered in the listener - this.listener.off('change'); - } - - // Change the horizontal and vertical screen status functions of the device - private changeOrientation(isLandscape: boolean) { - // Obtain the context of the UIAbility instance - let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext; - // Call this API to manually change the horizontal and vertical screen status of the device - window.getLastWindow(context).then((lastWindow) => { - lastWindow.setPreferredOrientation(isLandscape ? window.Orientation.LANDSCAPE : window.Orientation.PORTRAIT) - }); - } - - build() { - Column({ space: 50 }) { - Text(this.text) - .fontSize(50) - .fontColor(this.color) - Text('Landscape') - .fontSize(50) - .fontColor(this.color) - .backgroundColor(Color.Orange) - .onClick(() => { - this.changeOrientation(true); - }) - Text('Portrait') - .fontSize(50) - .fontColor(this.color) - .backgroundColor(Color.Orange) - .onClick(() => { - this.changeOrientation(false); - }) - } - .width('100%') - .height('100%') - } -} - -// [End Media] diff --git a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/OrientationPage.ets b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/OrientationPage.ets deleted file mode 100644 index 72e0ed10f9749da076c654256e9fcd7aa442986e..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/OrientationPage.ets +++ /dev/null @@ -1,91 +0,0 @@ -// [Start orientation] -import { display, window } from '@kit.ArkUI'; - -const TAG = 'foo'; -const ORIENTATION: Array = ['垂直', '平', '反向垂直', '反向水平']; - -@Entry -@Component -struct OrientationPage { - @State rotation: number = 0; - @State message: string = ORIENTATION[this.rotation]; - @Watch('setWindowLayOut') @State isLandscape: boolean = false; - - aboutToAppear() { - this.setOrientation(1); - let callback = async () => { - let getDefaultDisplay = display.getDefaultDisplaySync(); - this.rotation = getDefaultDisplay.rotation; - this.message = ORIENTATION[this.rotation]; - }; - try { - display.on('change', callback); - } catch (exception) { - hilog.error(0x0000, TAG, 'Failed to register callback. Code: ' + JSON.stringify(exception)); - } - } - - setOrientation(type: number) { - try { - window.getLastWindow(getContext(this), (err, data) => { - //获取window实例 - if (err.code) { - hilog.error(0x0000, TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(err)); - return; - } - let windowClass = data; - hilog.info(0x0000, TAG, 'Succeeded in obtaining the top window. Data: ' + JSON.stringify(data)); - let orientation: number; - if (type === 1) { - orientation = window.Orientation.AUTO_ROTATION; - } else { - orientation = window.Orientation.UNSPECIFIED; - } - try { - windowClass.setPreferredOrientation(orientation, (err) => { - if (err.code) { - hilog.error(0x0000, TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(err)); - return; - } - hilog.info(0x0000, 'Succeeded in setting window orientation.'); - }); - } catch (exception) { - hilog.error(0x0000, TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(exception)); - } - }); - } catch (exception) { - hilog.error(0x0000, TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(exception)); - } - } - - setWindowLayOut() { - window.getLastWindow(getContext(this)).then((windowClass) => { - if (this.isLandscape) { - hilog.info(0x0000, 'OrientationPage', '设置屏幕横屏'); - windowClass.setPreferredOrientation(window.Orientation.AUTO_ROTATION_LANDSCAPE); - } else { - hilog.info(0x0000, 'OrientationPage', '设置屏幕竖屏'); - windowClass.setPreferredOrientation(window.Orientation.AUTO_ROTATION_PORTRAIT); - } - }); - } - - build() { - Row() { - Column() { - Text(`${this.rotation}`) - .fontSize(25) - Text(`${this.message}`) - .fontSize(25) - Button('全屏') - .width(140) - .onClick(() => { - this.isLandscape = !this.isLandscape; - }) - } - .width('100%') - } - .height('100%') - } -} -// [End orientation] diff --git a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationA.ets b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationA.ets deleted file mode 100644 index 6d7948a9fef7441c53a005affe62aa8330bb79b3..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationA.ets +++ /dev/null @@ -1,25 +0,0 @@ -import { window } from '@kit.ArkUI'; - -@Component -struct ScreenRotationA { - // [Start orientation] - @StorageLink('mainWindow') mainWindow?: window.Window = undefined; - public lastOrientation?: window.Orientation; - - aboutToAppear(): void { - if (this.mainWindow === undefined) { - return; - } - this.lastOrientation = this.mainWindow!.getPreferredOrientation(); - this.mainWindow!.setPreferredOrientation(window.Orientation.LANDSCAPE); - } - - aboutToDisappear(): void { - this.mainWindow!.setPreferredOrientation(this.lastOrientation) - } - // [End orientation] - - build() { - Text('你好') - } -} diff --git a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationB.ets b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationB.ets deleted file mode 100644 index 80b24fa49d12fc2f810b0994086cfb12253423d8..0000000000000000000000000000000000000000 --- a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationB.ets +++ /dev/null @@ -1,20 +0,0 @@ -import { common } from '@kit.AbilityKit'; - -@Component -struct ScreenRotationB { - // [Start window] - private windowClass = (this.getUIContext().getHostContext() as common.UIAbilityContext).windowStage.getMainWindowSync() - - aboutToAppear(): void { - this.windowClass.enableLandscapeMultiWindow(); - } - - aboutToDisappear(): void { - this.windowClass.disableLandscapeMultiWindow(); - } - // [End window] - - build() { - Text('你好') - } -}