The App Linking service allows links that work across platforms even on devices where your app is not installed. You can use these links to direct users to promotional information or native app content that they can share with others. You can create links of App Linking and send them to users, 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:
For details, please refer to Preparations for Integration in AppGallery Connect.
If you are using Android Studio, you can integrate the App Linking SDK 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 address:
implementation ' com.huawei.agconnect:agconnect-applinking:1.4.1.300'
implementation ' com.huawei.hms:hianalytics:5.0.3.300'
}
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
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 links of App Linking.
When creating links of App Linking on the app client, you need to specify the URL prefix and deep link. In this codelab, the URL prefix and deep link have been configured.
private TextView shortTextView;
private TextView longTextView;
private static final String DOMAIN_URI_PREFIX = "https://applinkingtest.drcn.agconnect.link";
private static final String DEEP_LINK = "https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides";
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.huawei.agconnect.applinking.AppLinking;
import com.huawei.agconnect.applinking.AGConnectAppLinking;
TextView deeplinktext = findViewById(R.id.deepLink);
deeplinktext.setText(DEEP_LINK);
shortTextView = findViewById(R.id.shortLinkText);
longTextView = findViewById(R.id.longLinkText);
//creatButton
findViewById(R.id.create).setOnClickListener(view -> {
createAppLinking();
});
//shareButton
findViewById(R.id.shareShort).setOnClickListener(view -> {
shareLink((String) shortTextView.getText());
});
findViewById(R.id.shareLong).setOnClickListener(view -> {
shareLink((String) longTextView.getText());
});
AGConnectAppLinking.getInstance();
private void createAppLinking() {
AppLinking.Builder builder = new AppLinking.Builder().setUriPrefix(DOMAIN_URI_PREFIX)
.setDeepLink(Uri.parse(DEEP_LINK));
longTextView.setText(builder.buildAppLinking().getUri().toString());
builder.buildShortAppLinking(ShortAppLinking.LENGTH.SHORT).addOnSuccessListener(shortAppLinking -> {
shortTextView.setText(shortAppLinking.getShortUrl().toString());
}).addOnFailureListener(e -> {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
});
}
private void shareLink(String agcLink) {
if (agcLink != null) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, agcLink);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
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" />
<activity android:name=".AdsActivity"
android:label="Ads_Page">>
<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="developer.huawei.com" android:scheme="https" />
</intent-filter>
</activity>
AGConnectAppLinking.getInstance().getAppLinking(this,getIntent()).addOnSuccessListener(resolvedLinkData -> {
Uri deepLink = null;
if (resolvedLinkData != null) {
deepLink = resolvedLinkData.getDeepLink();
TextView textView = findViewById(R.id.textView);
textView.setText(deepLink.toString());
}
}).addOnFailureListener(e -> {
Log.i("AppLinking", "getAppLinking:onFailure", e);
});
Well done. You have successfully built an app that integrates App Linking of AppGallery Connect and learned how to:
For details about the development guide of App Linking, please refer to App Linking Service Introduction.
For details about APIs related to App Linking of AppGallery Connect, please refer to API Reference.
Download the demo source code used in this codelab from the following address: