gpt4 book ai didi

xamarin.android - 使用自定义 URL 方案通过浏览器启动 Xamarin Android 应用程序

转载 作者:行者123 更新时间:2023-12-02 07:27:19 26 4
gpt4 key购买 nike

程序

我有一个跨平台 Xamarin 项目,需要 URL 方案。要在 Xamarin Android 上启用此功能,我在 AndroidManifest.xml 中添加以下代码文件,引用launch custom android application发布。

<activity android:name=".MainActivity">
<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="urlschemetest" android:host="testurl" />
</intent-filter>
</activity>

打字 urlschemetest://testurl直接访问 Android 中的浏览器会转到 Google 搜索,而不是启动应用程序。所以,我有一个简单的 html具有打开应用程序的超链接的页面,test url scheme .

<a href="urlschemetest://testurl">open app</a>

问题

通过单击上面的超链接,该应用程序不会启动。 Visual Studio 显示 Unhandled Exception输出错误

Java.Lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.companyname.UrlSchemeTest/com.companyname.UrlSchemeTest.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.companyname.UrlSchemeTest.MainActivity" on path: DexPathList[[zip file "/data/app/com.companyname.UrlSchemeTest-1/base.apk"],nativeLibraryDirectories=[/data/app/com.companyname.UrlSchemeTest-1/lib/arm, /data/app/com.companyname.UrlSchemeTest-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]

尝试的解决方案

1) 我查看了 AndroidManifest.xml 中的属性 ,也许是因为 package 属性与 MainActivity.cs 中使用的 namespace 不匹配。 。所以我改变了它。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="UrlSchemeTest.Droid"
android:installLocation="auto">

MainActivity.cs

namespace UrlSchemeTest.Droid
{
[Activity(Label = "UrlSchemeTest", Icon = "@mipmap/icon",
Theme = "@style/MainTheme", MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity :global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
//body here
}
}

2) 在 <activity> 中使用绝对类名标签

<activity android:name="UrlSchemeTest.Droid.MainActivity">

3) 为了确定这是否是模拟器错误,我在 Android 模拟器和真实 Android 设备上测试了该应用程序。

注意:这个问题是关于让它在 Android 平台上工作的,尽管我已经创建了这个 Xamarin simple demo在 Android 和 iOS 上。

最佳答案

说明

最初,我认为事件名称是命名空间(或包名称)和类名称的组合。对于 native Android 和 Xamarin Android 5.1 之前的版本来说,情况确实如此。 Xamarin Android 5.1 及更高版本并非如此,如 docs 中所述。

Beginning with Xamarin.Android 5.1, the type name of an activity is based on the MD5SUM of the assembly-qualified name of the type being exported.

与我的Xamarin simple demo应用程序,如果您构建它并打开 UrlSchemeTest.Android/obj/Debug/android 文件夹中的 AndroidManifest.xml 文件,您将看到如下内容

<activity 
android:configChanges="orientation|screenSize"
android:icon="@mipmap/icon" android:label="UrlSchemeTest"
android:theme="@style/MainTheme"
android:name="md56cd34387ec86a2e1e9ba0754e1c30e2c.MainActivity">

如您所见,事件名称是 md5 和和类名称的组合。

因此,当我在 AndroidManifest.xml 中创建事件时,在构建时发生了冲突

解决方案

首先,我需要在 AndroidManifest.xml删除 Activity。 demo中需要删除以下代码

<activity android:name=".MainActivity">
<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="urlschemetest" android:host="testurl" />
</intent-filter>
</activity>

然后,在MainActivity.cs添加一个IntentFilter属性

using Android.App;
using Android.Content;

[IntentFilter(new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataScheme = "urlschemetest",
DataHost = "testurl")]
public class MainActivity :global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
//code here
}
}

网址方案现在应该可以工作了。

作为建议,我认为如果可以的话,我们应该尽量不要修改 AndroidManifest.xml

Xamarin.Android helps to minimize this difficulty by allowing you to add custom attributes to your classes, which will then be used to automatically generate the manifest for you. Our goal is that 99% of our users should never need to manually modify AndroidManifest.xml.

关于xamarin.android - 使用自定义 URL 方案通过浏览器启动 Xamarin Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51860967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com