EntryAbility.ets 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import AbilityConstant from '@ohos.app.ability.AbilityConstant';
  2. import hilog from '@ohos.hilog';
  3. import UIAbility from '@ohos.app.ability.UIAbility';
  4. import Want from '@ohos.app.ability.Want';
  5. import window from '@ohos.window';
  6. import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
  7. import { GlobalContext } from '../utils/GlobalThis';
  8. export default class EntryAbility extends UIAbility {
  9. onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
  10. GlobalContext.getContext().setObject("context", this.context);
  11. GlobalContext.getContext().setObject("pathDir", this.context.filesDir);
  12. }
  13. onDestroy(): void {
  14. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  15. }
  16. onWindowStageCreate(windowStage: window.WindowStage): void {
  17. let AtManager = abilityAccessCtrl.createAtManager();
  18. AtManager.requestPermissionsFromUser(this.context, ['ohos.permission.READ_MEDIA', 'ohos.permission.WRITE_MEDIA',
  19. 'ohos.permission.CAMERA', 'ohos.permission.MICROPHONE', 'ohos.permission.START_INVISIBLE_ABILITY']).then(() => {
  20. });
  21. // Main window is created, set main page for this ability
  22. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
  23. // 1.获取应用主窗口。
  24. let windowClass: window.Window
  25. windowStage.getMainWindow((err, data) => {
  26. if (err.code) {
  27. console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
  28. return;
  29. }
  30. windowClass = data;
  31. console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
  32. // 2.实现沉浸式效果:设置导航栏、状态栏不显示。
  33. let names: [] = [];
  34. windowClass.setWindowSystemBarEnable(names, (err) => {
  35. if (err.code) {
  36. console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err));
  37. return;
  38. }
  39. console.info('Succeeded in setting the system bar to be visible.');
  40. });
  41. windowClass.setWindowLayoutFullScreen(true)
  42. })
  43. // ================================= 更改跟路由
  44. windowStage.loadContent('pages/TestHdcInstallPage', (err) => {
  45. if (err.code) {
  46. hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
  47. return;
  48. }
  49. hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
  50. });
  51. }
  52. onWindowStageDestroy(): void {
  53. // Main window is destroyed, release UI related resources
  54. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  55. }
  56. onForeground(): void {
  57. // Ability has brought to foreground
  58. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  59. }
  60. onBackground(): void {
  61. // Ability has back to background
  62. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  63. }
  64. }