我目前正在尝试向使用 .NET MAUI 编写的 Android 应用程序添加深度链接支持(通过 Intents)。
我在 AndroidManifest.xml 的application元素下添加了一个活动XML 元素:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true">
<activity android:name="TestApp.MainActivity" android:exported="true">
<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:scheme="https"
android:host="test"
android:pathPrefix="/group" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Run Code Online (Sandbox Code Playgroud)
我还在Platforms\Android 下的 MainActivity.cs 中添加了一个IntentFilter (见下文):
[IntentFilter(new[] { Intent.ActionView },
Categories = new[]
{
Intent.ActionView,
Intent.CategoryDefault,
Intent.CategoryBrowsable
},
DataScheme = "https", DataHost = "test", DataPathPrefix = "/group"
)
]
public class MainActivity : MauiAppCompatActivity
Run Code Online (Sandbox Code Playgroud)
只是不确定此时要做什么来对意图做出反应(在哪里放置事件处理程序等)。
如果有人有任何建议,我们将不胜感激。
您可以intent通过重写OnNewIntent方法来处理。
从中获取信息intent.DataString并执行您想要的操作。
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//test
OnNewIntent(Intent);
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
var data = intent.DataString;
if (intent.Action != Intent.ActionView) return;
if (string.IsNullOrWhiteSpace(data)) return;
if (data.Contains("/group"))
{
//do what you want
}
}
Run Code Online (Sandbox Code Playgroud)