AGConnectServicesConfig只读取agconnect-services.第一次是Json



我正在将华为工具包(地图,分析,崩溃和推送)集成到我的Xamarin Forms应用程序中,但有奇怪的行为,agconnect-services。json仅在我第一次运行应用程序时读取,之后如果我使用此代码var appId = AGConnectServicesConfig.FromContext(this).GetString("client/app_id");取值为空。要再次读取配置文件,我必须卸载应用程序,即使在发布模式下,也会发生这种情况。我降级了所有来自华为的nuget,我使用https://github.com/johnthiriet/Xamarin.Android.Huawei.Hms.Demo的绑定,甚至我创建了自己的绑定,但似乎都不起作用。我正在使用文档中的内容提供程序和HmsLazyInputStream。

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

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

这里是官方NuGet包的链接和如何使用它的文档。

这个例子展示了如何使用map kit包和绑定,但是其他包和绑定在Github站点的样例项目中是可用的。

https://developer.huawei.com/consumer/en/doc/development/hms插件-指导- v1/integrating地图-工具- 0000001135925291 - v1

这里是您可以尝试的示例绑定项目。

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

最新更新