【问题标题】:Xamarin App won't run without google play services没有 Google Play 服务,Xamarin App 将无法运行
【发布时间】:2023-03-21 20:02:01
【问题描述】:

我是 Xamarin Android 的新手。我正在尝试在我的应用程序上显示一个基本的谷歌地图。我为谷歌地图 api 设置了 API 密钥等。但是,当我运行模拟器时,会显示一条消息“如果没有 google play 服务,应用将无法运行”。

我已使用 android sdk 管理器安装了所有软件包/SDK,我正在尝试使用 API 级别 22(Android 5.0 Lollipop)。

以下代码用于初始化谷歌地图(从 xamarin 示例下载)。

谁能告诉我我在这里缺少什么。

      using System;
      using Android.App;
      using Android.Content;
      using Android.Gms.Maps;
      using Android.Gms.Maps.Model;
      using Android.Runtime;
      using Android.Views;
      using Android.Widget;
      using Android.OS;

      namespace App2
      {
       [Activity(Label = "App2", MainLauncher = true, Icon = "@drawable/icon")]
      public class MainActivity : Activity, IOnMapReadyCallback
      {
        private GoogleMap _gMap;
        protected override void OnCreate(Bundle bundle)
        {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        GetMap();
    }

    private void GetMap()
    {

        if (_gMap == null)
        {
            FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map).GetMapAsync(this);
        }
    }

    public void OnMapReady(GoogleMap map)
    {
        _gMap = map;
    }
  }
}

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.MapFragment" />

Android Mainfest

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="App2.App2" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
<uses-sdk android:minSdkVersion="16" />
<application android:label="App2" android:icon="@drawable/Icon">
    <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyCKBrF-_IrAQlkUyFG7GT4qgEJ7qOgyNRI" />
</application>
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<!-- We need to be able to download map tiles and access Google Play Services-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Allow the application to access Google web-based services. -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Google Maps for Android v2 will cache map tiles on external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Google Maps for Android v2 needs this permission so that it may check the connection state as it must download data -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- These are optional, but recommended. They will allow Maps to use the My Location provider. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

【问题讨论】:

    标签: c# android .net google-maps xamarin


    【解决方案1】:

    这似乎是设备,或者在这种情况下是模拟器,没有安装 Google Play 服务。 Android 应用程序中的软件包仅提供对 Google Play 服务的访问,该服务通常安装在设备本身上。从这个意义上说,Google Play Services 更像是一个共享运行时。

    要解决此问题,您应该能够按照此guide 在模拟器上安装 Google Play 服务。

    我希望这会有所帮助!

    【讨论】:

    • 您好,clb,感谢您的回复。我按照您的说明下载了相应的 zip 文件,但问题是我无法将其拖放到播放器上,当我尝试将 zip 拖放到播放器上时,它会给我一个圆圈(禁止)图标。知道是什么原因造成的。
    • 嘿!我需要看看我是否可以在我的系统上重现它。一旦我这样做了,我会回复这个帖子
    【解决方案2】:

    您似乎正在尝试在未安装 Google Play 服务的模拟器映像上运行该应用程序。

    首先,我始终建议使用物理设备进行测试,但如果您需要测试您没有设备的各种 API 级别,那么使用模拟器就足够了。

    如果您使用的是 Google AVD,那么您可以 create a new virtual device 已经包含 Google Play 服务。

    如果您使用的是 Xamarin Android Player,那么您可以使用@clb 提到的this guide。但是,请记住 Xamarin Android Player 是预览软件,不受官方支持。如果这是您在安装 Google Play 服务时遇到问题的模拟器,那么您最好的支持选择是@​​987654323@。 (该指南当前显示错误的日志位置,现在通过 (Windows) 右键单击​​底部栏 (Mac) 单击帮助菜单 > 生成错误报告 - 日志将被压缩到您的桌面上)

    如果您仍然遇到问题,请告诉我您使用的是什么模拟器,我会看看是否可以提供进一步的帮助!

    【讨论】:

    • 嗨艾伦,我在物理设备上检查了我的应用程序,它运行良好。我想现在我将不得不忍受它,直到 Xamarin Player 发布新版本。另一种可能性可能是我正在运行没有 hyper-v 的 Windows 10 Home。
    【解决方案3】:

    正如其他人所说,问题似乎是您没有在模拟器上安装 Google Play 服务(又名 Google API)。这是我写的教程的链接,向您展示了如何做到这一点。还有一个视频:https://birdsbits.wordpress.com/2016/05/26/testing-location-aware-apps-on-an-emulator

    【讨论】:

    • 除了提供博客链接外,如果您在此处发布一些信息会很有帮助。链接可能会失效。
    • 关键是通过 SDK 管理器安装 Google API,然后在设置您的设备时选择它——特别是在 CPU/ABI 部分。
    猜你喜欢
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2014-11-18
    • 2015-08-14
    • 2017-12-08
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多