Xamarin is a popular cross platform framework to build mobile applications using .net
A number of AppGallery Connect services support many cross platform frameworks including Xamarin. Today we are going to take a look at how you can use one of these services, App Linking within your Xamarin project.
Enabling App Linking in AppGallery Connect
Create an app or use an existing app in AppGallery Connect. Click My projects, go to Grow > App Linking, and click Use now on the displayed page.
On the displayed App Linking page, click the URL prefixes tab and then click New URL prefix to create a unique URL prefix.
Preparing the Xamarin Android Development Environment
Completing Android Setup
Download the JSON file from AppGallery Connect and copy the file to your project's Assets directory.
Set a package name. Right-click your project and choose Properties. Click Android Manifest on the displayed page and set the same package name to that in the JSON file.
Implement LazyInputStream to read the agconnect-services.json file.
Right-click your project, and choose Add > New Item. In the displayed window, select Class and name the new class HmsLazyInputStream.cs.
The HmsLazyInputStream.cs class extends the LazyInputStream class and needs to be implemented. The sample code is as follows:
using System; | |
using System.IO; | |
using Android.Content; | |
using Android.Util; | |
using Huawei.Agconnect.Config; | |
namespace AppLinking1 | |
{ | |
public class HmsLazyInputStream : LazyInputStream | |
{ | |
public HmsLazyInputStream(Context context) | |
: base(context) | |
{ | |
} | |
public override Stream Get(Context context) | |
{ | |
try | |
{ | |
return context.Assets.Open("agconnect-services.json"); | |
} | |
catch (Exception e) | |
{ | |
Log.Error("Hms", $"Failed to get input stream" + e.Message); | |
return null; | |
} | |
} | |
} | |
} |
Create another new class as described in the preceding steps and read the agconnect-services.json file before your app is launched. You can name the new class CustomContentProvider.cs, which extends the ContentProvider class, and set the authorities and InitOrder attributes for the new class.
using System; | |
using Android.Content; | |
using Android.Database; | |
using Huawei.Agconnect.Config; | |
namespace AppLinking1 | |
{ | |
[ContentProvider(new string[] { "com.huawei.applinkingdemo.CustomContentProvider" }, InitOrder = 99)] | |
class CustomContentProvider : ContentProvider | |
{ | |
public override int Delete(Android.Net.Uri uri, string selection, string[] selectionArgs) | |
{ | |
throw new NotImplementedException(); | |
} | |
public override string GetType(Android.Net.Uri uri) | |
{ | |
throw new NotImplementedException(); | |
} | |
public override Android.Net.Uri Insert(Android.Net.Uri uri, ContentValues values) | |
{ | |
throw new NotImplementedException(); | |
} | |
public override bool OnCreate() | |
{ | |
AGConnectServicesConfig config = AGConnectServicesConfig.FromContext(Context); | |
config.OverlayWith(new HmsLazyInputStream(Context)); | |
return false; throw new NotImplementedException(); | |
} | |
public override ICursor Query(Android.Net.Uri uri, string[] projection, string selection, string[] selectionArgs, string sortOrder) | |
{ | |
throw new NotImplementedException(); | |
} | |
public override int Update(Android.Net.Uri uri, ContentValues values, string selection, string[] selectionArgs) | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
} |
Installing the Service SDK for Android
Right-click your project and choose Manage NuGet Packages.
Search for AppLinking on the Browse tab. Click Xamarin.Android bindings for AGC - Applinking in the search results and install it.
Agree to the service agreement as prompted.
Developing Your App
Creating an App Linking Link
To specify the layout of your app, open the activity_main file under Resources > layout. Sample code:
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout 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" | |
android:layout_margin="16dp" | |
android:orientation="vertical" | |
tools:context=".MainActivity"> | |
<TextView | |
android:id="@+id/textDeepLink" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="DeepLink:" | |
android:textSize="18sp" | |
android:textStyle="bold" /> | |
<TextView | |
android:id="@+id/deepLink" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
<Button | |
android:id="@+id/create" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Create Link" /> | |
<TextView | |
android:id="@+id/ShortLink" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Short Link:" | |
android:textSize="18sp" | |
android:textStyle="bold" /> | |
<TextView | |
android:id="@+id/textShortLink" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
<TextView | |
android:id="@+id/LongLink" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Long Link:" | |
android:textSize="18sp" | |
android:textStyle="bold" | |
/> | |
<TextView | |
android:id="@+id/textLongLink" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
<Button | |
android:id="@+id/shareShort" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Share Short Link" /> | |
<TextView | |
android:id="@+id/message" | |
android:layout_width="match_parent" | |
android:layout_height="80dp" /> | |
</LinearLayout> |
Open the MainActivity.cs file and import the following packages
using Android.App; | |
using Android.OS; | |
using Android.Runtime; | |
using Android.Widget; | |
using AndroidX.AppCompat.App; | |
using System; | |
using Huawei.Agconnect.Applinking; | |
using Uri = Android.Net.Uri; | |
using Debug = System.Diagnostics.Debug; | |
using Android.Content; |
Configure button actions in the onCreate method
Create an App Linking link and implement the method for sharing the link.
private AppLinking.Builder builder; | |
public static string longLink = null; | |
public static string shortLink = null; | |
public static string UriPrefix = "https://applinkingtest.drcn.agconnect.link"; | |
public static string OpenApp_Link = "https://open.androiddemoapp.com"; | |
public static string OpenDetail_Link = "https://open.androiddemoapp.com/detail?id=358"; | |
private async void CreateAppLink(object sender, EventArgs e) | |
{ | |
builder = new AppLinking.Builder(); | |
// Set a URL prefix. | |
builder.SetUriPrefix(UriPrefix); | |
// Set a deep link. | |
builder.SetDeepLink(Uri.Parse(OpenApp_Link)); | |
//Set the link preview type. If this method is not called, the preview page with app information is displayed by default. | |
builder.SetPreviewType(AppLinking.LinkingPreviewType.AppInfo); | |
// (Optional) Set Android link behavior. | |
var behaviorBuilder = new AppLinking.AndroidLinkInfo.Builder(); | |
// Set an earliest version. If a user's app version is earlier than the earliest version, your app will redirect the user to update the app on AppGallery. | |
behaviorBuilder.SetMinimumVersion(1); | |
builder.SetAndroidLinkInfo(behaviorBuilder.Build()); | |
longLink = builder.BuildAppLinking().Uri.ToString(); | |
FindViewById<TextView>(Resource.Id.textLongLink).Text = longLink; | |
} | |
private void ShareShortAppLink(object sender, EventArgs e) | |
{ | |
string agcLink = FindViewById<TextView>(Resource.Id.textShortLink).Text; | |
Intent intent = new Intent(Intent.ActionSend); | |
intent.SetType("text/plain"); | |
intent.PutExtra(Intent.ExtraText, agcLink); | |
intent.AddFlags(ActivityFlags.NewTask); | |
StartActivity(intent); | |
} |
Receiving an App Linking Link
Configure the code of the activity for receiving an App Linking link.
Right-click the project, choose add > New Item, select Activity, and name it DetailActivity.
The sample code is as follows:
using Android.App; | |
using Android.Content; | |
using Android.OS; | |
using Android.Runtime; | |
using Android.Views; | |
using Android.Widget; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Huawei.Agconnect.Applinking; | |
namespace AppLinking1 | |
{ | |
//[Activity(Label = "DetailActivity")] | |
[Activity(Name = "com.company.app.DetailActivity", Label = "DetailActivity", Theme = "@style/AppTheme")] | |
public class DetailActivity : Activity | |
{ | |
protected async override void OnCreate(Bundle savedInstanceState) | |
{ | |
base.OnCreate(savedInstanceState); | |
SetContentView(Resource.Layout.activity_detail); | |
// Create your app here. | |
try | |
{ | |
//To receive links, initialize the AGConnectAppLinking instance. | |
AGConnectAppLinking appLinkInstance = AGConnectAppLinking.Instance; | |
//Call GetAppLinkingAsync() to check links of App Linking to be processed | |
ResolvedLinkData resolvedLinkData = await appLinkInstance.GetAppLinkingAsync(this); | |
String deepLink = null; | |
if (resolvedLinkData != null) | |
{ | |
deepLink = resolvedLinkData.DeepLink.ToString(); | |
FindViewById<TextView>(Resource.Id.deepLink).Text = deepLink; | |
} | |
} | |
catch (System.Exception ex) | |
{ | |
FindViewById<TextView>(Resource.Id.message).Text = ex.Message; | |
} | |
} | |
} | |
} |
Configure the layout for the page of receiving an App Linking link.
Right-click the project, choose add > New Item, select Android Layout, and name it activity_detail. The sample code is as follows:
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout 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" | |
android:layout_margin="16dp" | |
android:orientation="vertical" | |
tools:context=".DetailActivity"> | |
<TextView | |
android:id="@+id/textDeepLink" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="DeepLink:" | |
android:textSize="18sp" | |
android:textStyle="bold" /> | |
<TextView | |
android:id="@+id/deepLink" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
<TextView | |
android:id="@+id/textMessage" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="message:" | |
android:textSize="18sp" | |
android:textStyle="bold" /> | |
<TextView | |
android:id="@+id/message" | |
android:layout_width="match_parent" | |
android:layout_height="80dp" /> | |
</LinearLayout> |
Configure the Manifest file. Find the Properties directory and open the AndroidManifest file in the directory. Configure the following content in the element.
<activity android:name="com.company.app.DetailActivity" > | |
<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="open.androiddemoapp.com" android:scheme="https" /> | |
<data android:host="open.androiddemoapp.com" android:scheme="http" /> | |
</intent-filter> | |
<!--App Linking SDK reads content on the clipboard each time the app is launched.--> | |
<meta-data android:name="com.huawei.agconnect.applinking.READ_CLIPBOARD_PERMISSION" android:value="Available" /> | |
</activity> |
Testing Your App
Click Run to test your app.
References
Getting started with Xamarin
App Linking (Android)
Top comments (0)