AgConnectServicesConfig仅读取AgConnect-Services.json首次
我正在将华为套件(地图,分析,崩溃和推动)集成到我的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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题与使用非官方绑定和包装有关。
这是指向官方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