【HarmonyOS】元服務和APP的相互跳轉、相互成就

【關鍵字】

卡片、跳轉、加桌

 

【背景介紹】

隨着鴻蒙生態的發展,各種類型的應用都已經可以在Harmony OS上無差異的運行,面對鴻蒙新興元服務的興起,各大廠家可能都在考慮一個問題:如果已經有APP了,有必要再開發一款元服務嗎?是化蛇添足還是畫龍點睛?元服務跟已有應用是什麼關係?維護工作會不會多?

回答問題之前我們先看一張圖:

unnaming.png

 

這些原生APP彷彿有一大堆話憋在肚子裏,試圖通過右上方的提示氣泡呼喚用戶,彷彿再說:點我,點我。反觀元服務這個標題黨簡直晃眼,並且元服務也是基於系統級API開發的,性能、設計、效果和流暢程度都可以比肩原生APP,然而 元服務也不是十項全能,從官方定義就能看出:原子化服務是HarmonyOS提供的一種面向未來的服務提供方式,是有獨立入口的(用戶可通過點擊方式直接觸發)、免安裝的(無需顯式安裝,由系統程序框架後臺安裝後即可使用)、可爲用戶提供一個或多個便捷服務的用戶應用程序形態。受限於HAP包大小限制,註定只能承載單一的業務。APP相比元服務能提供更完整、更豐富的應用體驗;

看完區別我們再回到上面問題(以打車類型應用爲例):

(一)通過APP引導用戶添加元服務到桌面,即可以讓用戶使用更加方便;也可以讓廠商推廣更加容易;並且只需要開發一些元服務的頁面;運維都使用現有服務的接口;

unnaming%20(1).png

 

技術實現:參照AGDS Preview Link,當前免費對外開放;

(二)充分利用元服務的各種接入方式和免安裝的特點搶先服務用戶,搶佔用戶;想想這年頭如果敲敲桌子就能點菜,還有人會去打開攝像頭瞄準對焦嗎?更別提下載個APP了;

unnaming%20(2).png

 

 

有了元服務再逐步引導用戶下載APP享用更豐富的APP能力,跟用戶深度綁定;

技術實現:創建demo工程,選擇JS語言開發,在index/index.hml中編寫頁面佈局

代碼如下:

<div class="container">
    <div class="copyText" onclick="goApp">
        <text>
            <span>more</span>
        </text>
    </div></div>

index/index.css編寫樣式

.container{
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 100%;
    width: 100%;
    background-color: aliceblue;
}
.go-div{
    width:400px;
    padding: 20px;
    justify-content:center;
    background-color:#EA545D;
    color:white;
    height: 100px;
    margin-bottom: 40px;
    margin-top: 20px;
    border-radius:20px;
}

跳轉實現代碼在index/index.js中實現

export default{
    data:{

    },
    async goApp(){
        var actionData = {};
        var action = {};
        action.bundleName = "com.h.j.hmservice";
        action.abilityName = "com.huawei.jsproject.ServiceAbility";
        action.messageCode = 1;
        action.data = actionData;
        action.abilityType = 0;
        action.syncOption = 1;
        await FeatureAbility.callAbility(action);

    }
}

新建ServiceAbility用來處理JS請求並實現跳轉邏輯

public class ServiceAbility extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");
    private MyRemote remote = new MyRemote();
    private Context context;

    @Override
    public IRemoteObject onConnect(Intent intent) {
        HiLog.info(LABEL_LOG, "IRemoteObject::onConnect");
        this.context = this;
        super.onConnect(intent);
        return remote.asObject();
    }
    class MyRemote extends RemoteObject implements IRemoteBroker {
        MyRemote() {
            super("MyService_MyRemote");
        }
        @Override
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
            System.out.println("test js Go");
            switch (code) {
                case 1: {
                    //如果APP已存在拉起APP
                    if (isAppExist(context, "com.petal.litegames")) {
                        Intent intent = new Intent();
                        Set<String> entities = new HashSet<>();
                        entities.add("android.intent.category.LAUNCHER");
                        Operation operation = new Intent.OperationBuilder()
                                .withDeviceId("")
                                .withBundleName("com.petal.litegames")
                                .withAction("android.intent.action.MAIN")
                                .withFlags(Intent.FLAG_NOT_OHOS_COMPONENT)
                                .withEntities(entities)
                                .build();
                        intent.setOperation(operation);
                        startAbility(intent);
                   //如果APP不存在跳轉到下載頁面
                    } else {
                        Intent intent = new Intent();
                        Operation operation = new Intent.OperationBuilder()
                                .withAction("android.intent.action.VIEW")
                                .withUri(Uri.parse("https://url.cloud.huawei.com/iwTjB76GHe?shareTo=qrcode"))
                                .withFlags(Intent.FLAG_ABILITY_NEW_MISSION)
                                .build();
                        intent.setOperation(operation);
                        startAbility(intent);
                    }
                    break;
                }
                default: {
                    Map<String, Object> result = new HashMap<String, Object>();
                    reply.writeString(ZSONObject.toZSONString(result));
                    return false;
                }
            }
            return true;
        }
        @Override
        public IRemoteObject asObject() {
            return this;
        }
    }
    boolean isAppExist(Context context, String appPkg) {
        try {
            IBundleManager manager = context.getBundleManager();
            return manager.isApplicationEnabled(appPkg);
        } catch (IllegalArgumentException e) {
            return false;
        }
    }}

把什麼功能放在元服務中需要開發者根據用戶使用場景各自劃分了,最後期待元服務百花齊放。

欲瞭解更多更全技術文章,歡迎訪問https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章