The App Linking service allows you to create links that work across multiple platforms including Android, iOS, and web. You can send promotion information to users with links of App Linking, or users can share links of App Linking dynamically generated in your app. Anyone who receives a link can tap it to access the linked content.
When a user taps a link of App Linking:
In this codelab, you will build an Android app that integrates App Linking. Your app will allow you to:
To integrate App Linking, you must complete the following preparations:
If you are using Android Studio, you can integrate SDKs by using the Maven repository into your Android Studio project before development.
// Configure the following address:
apply plugin: 'com.huawei.agconnect'
dependencies {
// Configure the following addresses:
implementation 'com.huawei.agconnect:agconnect-applinking:1.6.2.300'
implementation 'com.huawei.hms:hianalytics:6.3.2.300'
}
In this codelab, you can create a page in your Android Studio project and design the UI according to the following figure, with buttons to create and share a link of App Linking.
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to the Codelab"
android:textSize="24dp"
android:textAlignment="center"
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.1" />
<Button
android:id="@+id/create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create App Linking"
android:textAllCaps="false"
android:textSize="20sp"
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.2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Long App Linking:"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
<TextView
android:id="@+id/longLinkText"
android:layout_width="match_parent"
android:layout_height="80dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Short App Linking:"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
<TextView
android:id="@+id/shortLinkText"
android:layout_width="match_parent"
android:layout_height="60dp"
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.6" />
<Button
android:id="@+id/shareShort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share short App Linking"
android:textAllCaps="false"
android:textSize="20sp"
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.7" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="getLinkingResult:"
android:textSize="20sp"
android:textAllCaps="false"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.75" />
<TextView
android:id="@+id/result_text"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="15dp"
android:hint="Result"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.85" />
</androidx.constraintlayout.widget.ConstraintLayout>
When creating links of App Linking in your app, you need to specify the URL prefix and deep link. In this codelab, the URL prefix and deep link have been configured.
Open the MainActivity file under app/src/main/java/PACKAGE_NAME of your Android Studio project, and configure the following parameters in Class:
private static final String DOMAIN_URI_PREFIX = "https://codelabhard.drcn.agconnect.link";
private static final String DEEP_LINK = "https://developer.huawei.com/consumer/cn";
private static final String ANDROID_DEEP_LINK = "codelabhard://applinking.codelab/testid=123";
private static String App_LinkingUrl;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.huawei.agconnect.applinking.AGConnectAppLinking;
import com.huawei.agconnect.applinking.AppLinking;
// create Button
findViewById(R.id.create).setOnClickListener(view -> {
createAppLinking();
});
// share Button
findViewById(R.id.shareShort).setOnClickListener(view -> {
shareLink(App_LinkingUrl);
});
private void createAppLinking() {
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());
TextView longTextView = findViewById(R.id.longLinkText);
longTextView.setText(builder.buildAppLinking().getUri().toString());
builder.buildShortAppLinking().addOnSuccessListener(shortAppLinking -> {
Log.i("AppLinkingCodeLab", "buildShortAppLinking success");
TextView shortTextView = findViewById(R.id.shortLinkText);
App_LinkingUrl = shortAppLinking.getShortUrl().toString();
shortTextView.setText(App_LinkingUrl);
}).addOnFailureListener(e -> {
Log.i("AppLinkingCodeLab", "buildShortAppLinking failed: " + e.toString());
});
}
private void shareLink(String LinkingUrl) {
if (LinkingUrl != null) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, LinkingUrl);
startActivity(intent);
}
}
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Add the custom scheme of deep link. -->
<data android:host="applinking.codelab" android:scheme="codelabhard" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Add the URL prefix of App Linking. -->
<data android:host="photoplaza.drcn.agconnect.link" android:scheme="http"/>
<data android:host="photoplaza.drcn.agconnect.link" android:scheme="https"/>
</intent-filter>
signingConfigs {
release {
storeFile file('HmsDemo.jks')
keyAlias 'hmsdemo'
keyPassword '123456'
storePassword '123456'
v1SigningEnabled true
v2SigningEnabled true
}
}
buildTypes {
release {
signingConfig signingConfigs.release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
signingConfig signingConfigs.release
debuggable true
}
}
AGConnectAppLinking.getInstance().getAppLinking(this).addOnSuccessListener(resolvedLinkData -> {
Uri deepLink = null;
if (resolvedLinkData!= null) {
deepLink = resolvedLinkData.getDeepLink();
Log.i("AppLinkingCodeLab", "Open From App Linking: " + deepLink.toString());
TextView resultText = findViewById(R.id.result_text);
resultText.setText(deepLink.toString());
// Perform the following processing code for the deep link.
}
});
Well done. You have successfully built an app that integrates App Linking of AppGallery Connect and learned how to: