当前位置:网站首页>【AppLinking实战案例】通过AppLinking分享应用内图片
【AppLinking实战案例】通过AppLinking分享应用内图片
2022-07-02 07:16:00 【华为开发者论坛】
华为AppGallery Connect提供的App Linking服务支持,可以支持跨平台的分享,支持在Android,iOS,Web等多个平台上使用。对于Android和iOS的移动平台,可以直接拉起应用的指定页面,对于设备上没有安装指定应用的情况下,App Linking链接还可以拉起商店下载应用,下载好应用以后,首次打开也可以打开指定应用的界面。
下面介绍一个,通过华为 App Linking SDK,场景分享链接,用于分享应用内照片的实际使用场景。
需求分析
首先研究华为App Linking官方文档: App Linking-使用入门
从官方文档的步骤来看,集成华为App Linking服务,主要涉及四个步骤
- 在AGC管理台,创建对应的AGC项目。
- 在项目创建特定的链接前缀
- 在Android项目中集成App Linking SDK,并且通过代码灵活创建链接
- 配置Android应用接收并且处理App Linking 链接。
从代码要求和配置来看,仅需要提供对应的PhotoID,下游图片加载环节,就可以通过PhotoID实现照片的识别和加载。
因此,仅需要将PhotoID添加到App Linking的分享DeepLink中,然后接收的时候,通过对DeepLink的解析,来获取对应的PhotoID。
Step1:创建AGC项目
- 在AGC控制台,AppGallery Connect (huawei.com),界面选择我的项目。
- 在界面上选择 + 号 添加一个项目。输入对应的项目名称即可。
- 为项目添加对应的应用,此处需要配置对应包名,包名需要和Android项目代码中的保持一致。
Step2:创建链接前缀
- 在上一步创建的项目下,左侧菜单栏,选择 增长 – App Linking,进入到App Linking页签。
- 先点击立即使用, 开通该服务。
- 在页面下选择 “链接前缀” 页签,先添加一个链接前缀,这个前缀需要全网唯一,系统会自动帮你校验唯一性。
例如我配置的的链接前缀如下:
Step3:集成SDK、使用API创建链接
- 回到AGC项目设置页面,下载agconnect-service.json文件,放到Android项目的App路径下
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()));
}
Step4:接收App Linking连接。
先说一下规划,我计划在ImageDetailActivity这个页面,进行App Linking链接的接收,也就是说,链接直接拉起应用后,打开的是这个Detail页面。
- 配置AndroidManifest文件。找到ImageDetailActivity标签,为其添加需要接收DeepLink的Intent-filter
<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;
}
Step5:补充步骤
由于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
参考文档
- 华为AGC App Linking服务业务介绍:App Linking-业务介绍
- 华为ACG Android平台使用入门:App Linking-使用入门
- 华为AGC App Linking服务,常见问题排查:App Linking-视频课堂:常见问题排查
边栏推荐
- 点云投影图片
- MySQL environment configuration
- Pywin32 opens the specified window
- Analysis of hot spots in AI technology industry
- [SUCTF2018]followme
- Dialogue Wu Gang: why do I believe in the rise of "big country brands"?
- 12. Process synchronization and semaphore
- Shutter - canvas custom graph
- Oracle 笔记
- js promise.all
猜你喜欢
随机推荐
集成学习概览
Shutter - canvas custom graph
使用Windbg静态分析dump文件(实战经验总结)
[unity3d] production progress bar - make image have the functions of filled and sliced at the same time
PCL Eigen介绍及简单使用
Solutions to a series of problems in sqoop job creation
js数组常用方法
js promise.all
PCL之K-d树与八叉树
Thanos Receiver
KS009基于SSH实现宠物管理系统
In the face of uncertainty, the role of supply chain
14. Code implementation of semaphore
Mysql database remote access permission settings
Sus system availability scale
Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
Pywin32打开指定窗口
[pit avoidance guide] pit encountered using ugui: the text component cannot indent the first line by two spaces
JSP webshell免杀——JSP的基础
Ks009 implement pet management system based on SSH