AgConnectServicesConfig仅读取AgConnect-Services.json首次

发布于 2025-02-12 02:29:40 字数 3120 浏览 1 评论 0 原文

我正在将华为套件(地图,分析,崩溃和推动)集成到我的Xamarin表单应用中,但是有奇怪的行为,AgConnect-Services.JSON仅在我第一次运行该应用程序时才读取,如果我使用该应用程序此代码 var appid = agConnectServicesConfig.fromContext(this).getString(“ client/app_id”); > 该值为null。要再次读取配置文件,我也必须卸载该应用程序,即使在发布模式下,也会发生这种情况。 我从华为降低了所有nuget,我使用

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;
            }
        }
    }
[ContentProvider(new string[] { "com.test.qa.XamarinCustomProvider" }, InitOrder = 99)]
    public class XamarinCustomProvider : 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;
        }

        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();
        }
    }

MainActivity.cs中的初始化

if (useHMSService)
           {
               AGConnectCrash.Instance.SetUserId("Anonymous user");
               HiAnalyticsTools.EnableLog();
               var instance = HiAnalytics.GetInstance(this);
               instance.SetAnalyticsEnabled(true);
               instance.SetReportPolicies(new List<ReportPolicy> { ReportPolicy.OnAppLaunchPolicy });

               MapsInitializer.SetApiKey("APIKEY");
               HmsMessaging.GetInstance(this).AutoInitEnabled = true; //Init push notifications

               var appId = AGConnectServicesConfig.FromContext(this).GetString("client/app_id");
               if (!string.IsNullOrWhiteSpace(appId))
               {
                   AlertDialog.Builder alert = new AlertDialog.Builder(this);
                   alert.SetTitle("AppID");
                   alert.SetMessage(appId);

                   Dialog dialog = alert.Create();
                   dialog.Show();
               }

           }

I'm integrating the Huawei Kit (Maps, Analytics, Crash and Push) into my Xamarin Forms App, but there is strange behavior, the agconnect-services.json is read only the first time I run the app, after that if I use this code var appId = AGConnectServicesConfig.FromContext(this).GetString("client/app_id");
the value is null. To read the configuration file again I have to uninstall the app, even in release mode, this happens.
I downgraded all the nuget from Huawei, I use the bindings from https://github.com/johnthiriet/Xamarin.Android.Huawei.Hms.Demo, even I created my own bindings but nothing seems to work.
I am using the Content Provider and the HmsLazyInputStream from the documentation.

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", 
quot;Failed to get input stream" + e.Message);
                return null;
            }
        }
    }
[ContentProvider(new string[] { "com.test.qa.XamarinCustomProvider" }, InitOrder = 99)]
    public class XamarinCustomProvider : 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;
        }

        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();
        }
    }

Initialization in MainActivity.cs

if (useHMSService)
           {
               AGConnectCrash.Instance.SetUserId("Anonymous user");
               HiAnalyticsTools.EnableLog();
               var instance = HiAnalytics.GetInstance(this);
               instance.SetAnalyticsEnabled(true);
               instance.SetReportPolicies(new List<ReportPolicy> { ReportPolicy.OnAppLaunchPolicy });

               MapsInitializer.SetApiKey("APIKEY");
               HmsMessaging.GetInstance(this).AutoInitEnabled = true; //Init push notifications

               var appId = AGConnectServicesConfig.FromContext(this).GetString("client/app_id");
               if (!string.IsNullOrWhiteSpace(appId))
               {
                   AlertDialog.Builder alert = new AlertDialog.Builder(this);
                   alert.SetTitle("AppID");
                   alert.SetMessage(appId);

                   Dialog dialog = alert.Create();
                   dialog.Show();
               }

           }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月无声 2025-02-19 02:29:40

您的问题与使用非官方绑定和包装有关。

这是指向官方Nuget软件包的链接和有关如何使用它的文档。

该示例显示了如何使用MAP KIT软件包和绑定,但是在GitHub站点的示例项目上可以使用其他软件包和绑定。

Here the sample binding project that you can try out.

https://github.com/hms-core/hms-core/hms-xamarin-bindings

Your problem is related to using un-official binding and package.

Here is the link to the official NuGet package and document for how to use it.

The example show how to use map kit package and binding, but others packages and binding are available on the sample project at Github site.

https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides-V1/integrating-map-kit-0000001135925291-V1

Here the sample binding project that you can try out.

https://github.com/HMS-Core/hms-xamarin-bindings

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文