华为AppGallery Connect提供的App Linking服务支持,可以支持跨平台的分享,支持在Android,iOS,Web等多个平台上使用。对于Android和iOS的移动平台,可以直接拉起应用的指定页面,对于设备上没有安装指定应用的情况下,App Linking链接还可以拉起商店下载应用,下载好应用以后,首次打开也可以打开指定应用的界面。
下面介绍一个,通过华为 App Linking SDK,场景分享链接,用于分享应用内照片的实际使用场景。
首先研究华为App Linking官方文档: App Linking-使用入门
从官方文档的步骤来看,集成华为App Linking服务,主要涉及四个步骤
从代码要求和配置来看,仅需要提供对应的PhotoID,下游图片加载环节,就可以通过PhotoID实现照片的识别和加载。
因此,仅需要将PhotoID添加到App Linking的分享DeepLink中,然后接收的时候,通过对DeepLink的解析,来获取对应的PhotoID。

例如我配置的的链接前缀如下:


2. 打开Android项目级gradle文件,添加如下AGC Maven仓地址和AGCP插件。
- buildscript {
- repositories {
- google()
- jcenter()
- maven { url 'http://developer.huawei.com/repo/' }
- }
- dependencies {
- classpath "com.android.tools.build:gradle:4.1.1"
- classpath 'com.huawei.agconnect:agcp:1.6.0.300'
- // NOTE: Do not place your application dependencies here; they belong
- // in the individual module build.gradle files
- }
- }
- allprojects {
- repositories {
- google()
- jcenter()
- maven { url 'http://developer.huawei.com/repo/' }
- }
- }
3. 在Android应用级gradle文件中,应用AGCP插件,并且添加App Linking SDK。
- apply plugin: 'com.android.application'
- apply plugin: 'com.huawei.agconnect'
-
- android{…}
-
-
- dependencies {
- ….
- implementation 'com.huawei.agconnect:agconnect-applinking:1.6.0.300'
- }
4. 使用API接口,根据PhotoID灵活创建App Linking链接:
此处需要注意的是,我传入了UserName和ImageURL参数,作为分享时候的预览页,另外将PhotoID这个参数封装到了DeepLink里。
- private static final String DOMAIN_URI_PREFIX = "https://photoplaza.drcn.agconnect.link";
- private static final String DEEP_LINK = "https://photoplaza-agc.dra.agchosting.link";
- private static final String SHARE_DEEP_LINK = "photo://photoplaza.share.com";
- /**
- * Call AppLinking.Builder to create a App Linking in App
- *
- * @param UserName input UserName
- * @param PhotoID input PhotoID
- * @param ImageUrl input ImageUrl
- * @param icallback input icallback
- */
- public void createShareLinking(String UserName, String PhotoID, String ImageUrl, Icallback icallback) {
- String newDeep_Link = SHARE_DEEP_LINK + "?PhotoID=" + PhotoID;
- AppLinking.Builder builder = AppLinking.newBuilder()
- .setUriPrefix(DOMAIN_URI_PREFIX)
- .setDeepLink(Uri.parse(DEEP_LINK))
- .setAndroidLinkInfo(AppLinking.AndroidLinkInfo.newBuilder()
- .setAndroidDeepLink(newDeep_Link)
- .build())
- .setSocialCardInfo(AppLinking.SocialCardInfo.newBuilder()
- .setTitle("It is a beautiful Photo")
- .setImageUrl(ImageUrl)
- .setDescription(UserName + " share a Photo to you")
- .build())
- .setCampaignInfo(AppLinking.CampaignInfo.newBuilder()
- .setName("UserSharePhoto")
- .setSource("ShareInApp")
- .setMedium("WeChat")
- .build());
- builder.buildShortAppLinking().addOnSuccessListener(shortAppLinking -> {
- shortLink = shortAppLinking.getShortUrl().toString();
- try {
- icallback.onSuccess(shortLink, "Success");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }).addOnFailureListener(e -> icallback.onFailure(e.getMessage()));
- }
先说一下规划,我计划在ImageDetailActivity这个页面,进行App Linking链接的接收,也就是说,链接直接拉起应用后,打开的是这个Detail页面。
- <activity android:name=".ImageDetailActivity">
- <meta-data
- android:name="android.support.PARENT_ACTIVITY"
- android:value=".ImageListActivity" />
- <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="photoplaza.share.com" android:scheme="photo" />
- </intent-filter>
- </activity>
2. 打开ImageDetail文件,在onCreate()中配置接收并且解析App Linking并且获取DeepLink的相关代码
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setDarkStatusIcon();
- setContentView(R.layout.image_detail);
- AGConnectAppLinking.getInstance().getAppLinking(ImageDetailActivity.this)
- .addOnSuccessListener(resolvedLinkData -> {
- // 通过AppLinking启动应用,此处适配已安装场景的再次启动
- if (resolvedLinkData!= null) {
- Uri deepLink = resolvedLinkData.getDeepLink();
- Log.i(TAG, "Open From AppLinking:" + deepLink);
- // Get picture details.
- getSharePicDetails(deepLink.toString());
- }
- }).addOnFailureListener(e->{
- Bundle data = getIntent().getExtras();
- if (data != null) {
- if (data.getBoolean("firstLink")) {
- // 通过AppLinking首次启动应用的入口
- getSharePicDetails(data.getString("deepLink"));
- }
- else{
- // 正常打开ImageDetail页面
- initView();
- }
- }
- });
- }
-
- /**
- * Get a picture detail from a App Linking which share from other.
- *
- * @param link input a deepLink of App Linking
- */
- private void getSharePicDetails(String link) {
- ToastUtils.showToast(this, getResources().getString(R.string.loading_photo));
- sharePhotoID = parseLink (link).get("PhotoID");
- Log.i("AppLinking", "sharePhotoID: " + sharePhotoID);
- }
-
- /**
- * Get param in received deepLink from AppLinking.
- *
- * @param url input the received deepLink
- * @return myMap output the param
- */
- public static Map<String, String> parseLink(String url) {
- Map<String, String> myMap = new HashMap<String, String>();
- String[] urlParts = url.split("\\?");
- String[] params = urlParts[1].split("&");
- for (String param : params) {
- String[] keyValue = param.split("=");
- myMap.put(keyValue[0], keyValue[1]);
- }
- return myMap;
- }
由于App Linking还会使用在应用未安装的场景。该步骤特定用户介绍全新安装的场景下 ,App Linking需要做何种适配。
首先对于应用未安装情况,App Linking链接会根据指定的包名(默认就是创建链接时候的那个App的包名), 根据包名,打开你手机的华为商店的应用下载页面,当然也可以配置本地商店打开。
在商店下载并且安装好以后,点击打开,App Linking也是生效并且可以传递参数的。但是由于是首次打开,此时第四步配置的Intent-filter是不生效的。只能打开应用的首页。因为该步骤的重点,就是在应用的启动页里面,适配App Linking首次打开的场景。
2.代码适配,打开应用启动页,同样配置getAppLinking的代码
例如我的启动页面是LoginActivity,
- AGConnectAppLinking.getInstance().getAppLinking(LoginActivity.this).addOnSuccessListener(resolvedLinkData -> {
- // 通过AppLinking首次启动
- Log.i(TAG,"StartUp From AppLinking");
- if (resolvedLinkData!= null) {
- String deepLink = resolvedLinkData.getDeepLink().toString();
- Log.i("AppLinking", "deepLink:" + deepLink);
- Bundle bundle = new Bundle();
- bundle.putBoolean("firstLink", true);
- bundle.putString("deepLink", deepLink);
- Intent intent;
- intent = new Intent(LoginActivity.this, ImageDetailActivity.class);
- intent.putExtras(bundle);
- startActivity(intent);
- finish();
- }
- }).addOnFailureListener(e-> {
- // 普通的启动方式
- Log.i(TAG,"Normal StartUp");
- initView();
- });
上面,就是Android项目内,通过App Linking 的SDK,快速构建应用内图片分享的能力,不需要额外申请应用专属域名,也不涉及后台的运维、H5页面的开发、JavaScript代码的适配等内容,轻松五个步骤就完成了。可以说非常方便快捷。
下面是涉及的页面的简单步骤截图:

欲了解更多详情,请参见:
华为官网:
https://developer.huawei.com/consumer/cn/forum/topic/0202652719220650728?fid=0101271690375130218?ha_source=zzh
参考文档