How to open the camera and explicitly open the UIAbility in the device [Nut Pie-Nut]

How to open the camera and explicitly open the UIAbility in the device [Nut Pie-Nut]

I saw the troublesome programmers asking for help in the Laval community today

Original link:

https://laval.csdn.net/64e69d6c2ea0282871eaa75d.html

image-20230825081852046

The role of the non-governmental organization Nut Pie came out.

Here I will introduce to you the want that will be used later.

Want

Want is the carrier of information transfer between objects, and can be used for information transfer between application components. One of the usage scenarios of Want is as a parameter of startAbility, which contains the specified startup target and related data to be carried when starting, for example, the bundleName and abilityName fields respectively indicate the package name of the application where the target Ability is located and the name of the Ability in the corresponding package . When UIAbilityA needs to start UIAbilityB and pass in some data, it can use Want as a carrier to pass these data to UIAbilityB.

next we will start

how to turn on the camera

Step 1: Import the module

import common from '@ohos.app.ability.common';

Step Two: Get Context

  let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext

Step 3: Add want information

 let want = {
    deviceId: '', // deviceId为空表示本设备
    bundleName: 'com.ohos.camera',
    abilityName: 'com.ohos.camera.MainAbility'
  };

Step Four: Start

  context.startAbility(want, (err) => {
    console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
  });

full source code

import common from '@ohos.app.ability.common';

@Entry
@Component
struct Index {


  build() {
    Row() {
      Column() {
       

        Text("显式打开相机")
          .fontSize(36)
          .fontWeight(FontWeight.Bold).onClick(() => {

          let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext

          let want = {
            deviceId: '', // deviceId为空表示本设备
            bundleName: 'com.ohos.camera',
            abilityName: 'com.ohos.camera.MainAbility'
          };

          context.startAbility(want, (err) => {
            console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
          });


        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

Let's look at another question from him

image-20230825082333131

https://laval.csdn.net/64e566284165333c3076a6a1.html

Explicitly enable in-device UIAbility

Step 1: Import the module

import common from '@ohos.app.ability.common';

Step Two: Get Context

  let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext

Step 3: Add want information

let want = {
 
         deviceId: '', // deviceId为空表示本设备
            bundleName: 'com.example.map',
            abilityName: 'EntryAbility',
          };

Step Four: Start

  context.startAbility(want, (err) => {
    console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
  });

full source code

import common from '@ohos.app.ability.common';

@Entry
@Component
struct Index {


  build() {
    Row() {
      Column() {
       

    Text("显式打开设备内UIAbility")
          .fontSize(36)
          .fontWeight(FontWeight.Bold).onClick(() => {

          let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext

          let want = {
 
         deviceId: '', // deviceId为空表示本设备
            bundleName: 'com.example.map',
            abilityName: 'EntryAbility',
          };

          context.startAbility(want, (err) => {
            console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
          });


        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

Ok, that's the end of this article.

Welcome everyone to join Nuts and build the Hongmeng Ecology together.

Guess you like

Origin blog.csdn.net/qq_39132095/article/details/132487543