HarmonyOS鸿蒙Next中跳转到指定URI进行访问

HarmonyOS鸿蒙Next中跳转到指定URI进行访问

本节演示,如何通过设置Intent,来从当前应用跳转到指定URI进行访问。

创建应用

创建名为IntentOperationWithUri的应用。

修改MainAbilitySlice

修改MainAbilitySlice代码如下:

package com.waylau.hmos.intentoperationwithuri.slice;

import com.waylau.hmos.intentoperationwithuri.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;

public class MainAbilitySlice extends AbilitySlice {
    private static final String TAG = MainAbilitySlice.class.getSimpleName();
    private static final HiLogLabel LABEL_LOG =
        new HiLogLabel(HiLog.LOG_APP, 0x00001, TAG);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 添加点击事件来触发请求
        Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
        text.setClickedListener(listener -> this.goToSearch());
    }

    private void goToSearch() {
        HiLog.info(LABEL_LOG, "before goToSearch");
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withUri(Uri.parse("https://www.baidu.com/ ")) // 指定要访问的URI
            .build();
        intent.setOperation(operation);

        // 启动Ability
        startAbility(intent);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

上述代码

  • 对默认生成的Id_text_helloworld文本设置点击事件,点击则会执行goToSearch方法。
  • goToSearch构建了Intent,该对象指明了withUri为待访问的URI。
  • 使用startAbility方法来启动Ability。

运行

运行应用,界面显示如下。点击“Hello World”本文,触发点击事件。

此时,界面切换到了搜索界面,如下。

cke_149.png

用户可以根据上述界面所提供的具备搜索的应用来执行下步访问操作。

比如选择浏览器。界面如下

cke_150.png

源码

上述示例源码,可以在https://github.com/waylau/harmonyos-tutorial仓库找到。


更多关于HarmonyOS鸿蒙Next中跳转到指定URI进行访问的实战教程也可以访问 https://www.itying.com/category-93-b0.html

12 回复

ArkUI 能用吗

更多关于HarmonyOS鸿蒙Next中跳转到指定URI进行访问的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


import com.waylau.hmos.intentoperationwithuri.ResourceTable;

没找到

  • 项目名称: 你自己的工程包
  • 版本号: v1.0.0
  • 描述: 这是一个示例项目,用于演示如何将HTML内容转换为Markdown格式。
  • 作者: John Doe
  • 创建日期: 2023-10-01
  • 最后修改日期: 2023-10-02
  • 状态: 开发中

能否讲一讲如何使用这个库,okhttp,下载一个txt 文件

https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp

// https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp

implementation group: ‘com.squareup.okhttp3’, name: ‘okhttp’, version: ‘4.9.3’

另外,如何使用 https://github.com/shashwatak/satellite-js 这个库,如何将他加入到HUAWEI DevEco Studio中

还有,JS UI 环境开发的.js文件,如何使用以前别人java写的程序?

谢谢

js文件是不能使用java写的程序,

真让我给搜到了,谢谢!谢谢!

是的,多谢支持。关注我,不迷路~

等我放假回来一定好好学习

感谢支持,请期待后续的更新

学习了吗,

基本信息

  • 课程名称: Python入门
  • 教师: 张三
  • 时间: 2023-04-01
  • 地点: 线上

在HarmonyOS鸿蒙Next中,你可以使用Intent来跳转到指定的URI进行访问。以下是一个示例代码:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setUri(Uri.parse("https://www.example.com"));
startAbility(intent);

这段代码会启动系统默认的浏览器或其他支持ACTION_VIEW的应用来打开指定的网页。你可以根据需要修改URI来访问不同的资源。

回到顶部