隐藏

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

发布:2021/10/13 17:03:25作者:管理员 来源:本站 浏览次数:815

程序


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 直接进入安卓系统的浏览器将进入谷歌搜索,而不是启动应用程序。所以,我有一个简单的 html 具有打开应用程序的超链接的页面, test url scheme


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


问题


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


   Java.Lang.RuntimeException:无法实例化活动 ComponentInfo{com.companyname.UrlSchemeTest/com.companyname.UrlSchemeTest.MainActivity}: java.lang.ClassNotFoundException:未找到类 文件 “/data/app/com.companyname.UrlSchemeTest-1/base.apk”],nativeLibraryDirectories=[/data/app/com.companyname.UrlSchemeTest-1/lib/arm, /供应商/lib,/system/lib]]


尝试的解决方案


属性 在里面 AndroidManifest.xml文件 ,也许是因为 包裹 命名空间 用于 MainActivity.cs . 所以我改了。


<manifest xmlns:android="http://schemas.android.com/apk/res/android"

  android:versionCode="1"

  android:versionName="1.0"

  package="UrlSchemeTest.Droid"

  android:installLocation="auto">


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上。

解释


docs


   从Xamarin.Android 5.1开始,活动的类型名基于要导出的类型的程序集限定名的MD5SUM。


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


<activity

   android:configChanges="orientation|screenSize"

   android:icon="@mipmap/icon" android:label="UrlSchemeTest"

   android:theme="@style/MainTheme"

   android:name="md56cd34387ec86a2e1e9ba0754e1c30e2c.MainActivity">


因此,当我在 AndroidManifest.xml文件

解决方案


删除活动 在里面 AndroidManifest.xml文件


<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>


那么, 一 IntentFilter 财产 MainActivity.cs


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

   }

}


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


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