• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

快速集成华为AGCAppLinking服务Android平台

android 搞代码 3年前 (2022-03-02) 20次浏览 已收录 0个评论
文章目录[隐藏]

最近,我的利用须要应用跨平台的分享链接,刚好华为AppGallery Connect的AppLinking服务满足我的应用场景。

对于集成步骤,官网的材料写的有点多,我总结一下步骤
i. 步骤一:创立利用,开明AppLinking服务

ii. 步骤二:创立一个链接前缀

iii. 步骤三:在Android我的项目里集成AppLinking SDK;

iv. 步骤四:创立AppLinking

v. 步骤五:接管AppLinking链接并且测试。

1、创立利用,开明AppLinking服务

在AGC控制台,创立利用, 或者应用已有的利用,在界面上找到 我的我的项目 -> 增长–>AppLinking,点击立刻开明 。

开明好当前,记得去 我的我的项目 -> 我的项目设置–> 惯例 上面,下载agconnect-services.json文件到你的Android我的项目的app门路下。

2、创立一个链接前缀

在刚刚开明的AppLinking上面,点击链接前缀页签,点击增加链接前缀,依据须要创立一个现网惟一的前缀。

零碎会主动帮你检测,保障你域名的全网惟一。

3、在Android我的项目外面集成AppLinking SDK

配置SDK地址,关上Android工程,在我的项目级build.gradle文件中,配置如下内容

<code class="java">buildscript {
repositories {
//….
        maven {url 'https://developer.huawei.com/repo/'}
    }
    dependencies {
        //….
        classpath 'com.huawei.agconnect:agcp:1.4.1.300'![在这里插入图片形容](https://img-blog.csdnimg.cn/20201229190623817.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDcwODI0MA==,size_16,color_FFFFFF,t_70#pic_center)

    }
}
 
allprojects {
    repositories {
//….
        maven {url 'https://developer.huawei.com/repo/'}
    }
}

关上利用级的build.gradle文件,配置好AppLinking和华为剖析的SDK,配置上面标红的内容即可

<code class="java">…
apply plugin: 'com.huawei.agconnect'
 ...
dependencies {
 ...
implementation "com.huawei.agconnect:agconnect-applinking:1.4    
implementation 'com.huawei.hms:hianalytics:5.0.4.301'.1.300"
}

4、创立AppLinking

有两种形式创立AppLinking: 一种是间接在AGC界面上创立,另外一个是在Android我的项目外面用代码的API接口创立。

4.1 AGC界面创立AppLinking:

1、界面入口如下:点击创立AppLinking,而后依据步骤一步一步创立即可。

2、默认的深度链接配置,我就间接随便找了一个华为官网的。留神Android的深度链接的配置。

3、安卓链接行为,配置为:在Android利用中关上。

创立好当前,就能够复制下来应用了

4.2 端侧代码创立AppLinking

1、在activity_main.xml外面先把界面配置好:整两个按钮,一个创立,一个分享;再创立一个TextView显示框,用来显示AppLinking。

<code class="java"><Button
        android:id="@+id/create"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Create App Linking"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.4" />
 
    <TextView
        android:id="@+id/showLink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="your App Linking"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5" />
 
    <Button
        android:id="@+id/shareLink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Share App Linking"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.8" />

如下图所示:

2、而后把刚刚创立的链接前缀,复制增加到常量,另外再先把须要关上的DeepLink地址配置好。

<code class="java">private static final String DOMAIN_URI_PREFIX = "https://testapplinking1016.drcn.agconnect.link";
 private static final String DEEP_LINK = " https://consumer.huawei.com/cn/";
private static final String Android_DEEP_LINK = "myapp://testapplinking/?data=1016";
 private String shortLink;

2、在MainActivity的OnCreate外面,利用create按钮创立AppLinking

<code class="java">findViewById(R.id.create).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
                 AppLinking.Builder builder = new AppLinking.Builder()
                         .setUriPrefix(DOMAIN_URI_PREFIX)
                         .setDeepLink(Uri.parse(DEEP_LINK))
                         .setAndroidLinkInfo(new AppLinking.AndroidLinkInfo.Builder()
.setAndroidDeepLink(Android_DEEP_LINK).build());.build());
 
                 builder.buildShortAppLinking().addOnSuccessListener(shortAppLinking -> {
                         shortLink = shortAppLinking.getShortUrl().toString();
                         TextView showAppLinking = findViewById(R.id.showLink);
                         showAppLinking.setText(shortLink);
                 }).addOnFailureListener(e -> {
                         Log.e("AppLinking", "Failure + "+ e.getMessage());
                 });
    }
});

3、在用shareLink按钮将刚刚创立的AppLinking分享进来

<code class="java">findViewById(R.id.shareLink).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, shortLink);
        startActivity(intent);
    }
});

5、接管相干AppLinking

接管的时候有两步操作,一个是须要配置manifest文件,另外一个在链接的入口配置getAppLinking办法:
1、 配置manifest文件:留神这里是将DeepLink的域名的Scheme配置进去:
比方我这里的DeepLink是:DEEP_LINK = “myapp://testapplinking/?data=1016”;
private static final String DEEP_LINK = “myapp://testapplinking/?data=1016”;

那么manifest文件就须要这样配置

<code class="java"><intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:host="testapplinking" android:scheme="myapp" />
</intent-filter>

2、在OnCreate的主入口外面,配置getAppLinking,获取并且显示链接

<code class="java">AGConnectAppLinking.getInstance().getAppLinking(this).addOnSuccessListener(resolvedLinkData -> {
        if (resolvedLinkData != null) {
                 String Result = resolvedLinkData.getDeepLink().toString();
                 TextView showAppLinking = findViewById(R.id.showLink);
                 showAppLinking.setText(Result);
        }
});

6、打包测试,查看景象。

1、利用运行当前,点击Create按钮,创立一个AppLinking链接,显示在界面上。

2、 点击Share按钮,将AppLinking链接分享到便签外面暂存,而后,在便签里点击链接,通过浏览器关上。浏览器能够间接关上利用,测试实现。

(从界面上创立的AppLinking也是一样的,能够先复制到便签外面,而后通过便签点击测试)

欲了解更多详情,请参见:

  https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-applinking-introduction

7、总结

集成简略,SDK依赖体积小,能够实现跨平台的分享,Android和iOS都能够反对,不须要在不同的平台做不同的适配了,节约工作量。

经营做推广能够再AGC界面上创立,开发做分享性能能够在端侧用代码创立,几乎完满。

参考文档:

华为AGC AppLinking服务开发文档:https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-applinking-introduction

原文链接:https://developer.huawei.com/consumer/cn/forum/topic/0204406653695530273?fid=0101271690375130218

原作者:Jessyyyyy


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:快速集成华为AGCAppLinking服务Android平台

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址