gpt4 book ai didi

android - 设置位置管理器 RequestLocationUpdate

转载 作者:行者123 更新时间:2023-11-29 20:59:30 25 4
gpt4 key购买 nike

我是 Android 和 Xamarin 世界的新手。我正在尝试为位置管理器创建一个单独的类以用于不同的 Activity 。这是我的代码

using System;
using Android.Content;
using Android.Locations;


namespace MyLocation
{
public class LocationManagerActivity
{
private LocationManager _locationManager;
private Geocoder _geocoder;
string bestProvider;
private Context locationContext;

public LocationManagerActivity (Context lContext)
{
locationContext = lContext;
}

private void getCurrentLocation()
{
string provider = LocationManager.GpsProvider;

if (_locationManager.IsProviderEnabled (provider)) {

_locationManager.RequestLocationUpdates (1000, 100, null, locationContext);
}
}

private void InitializeLocationManager()
{
_geocoder = new Geocoder (locationContext);

_locationManager = (LocationManager)locationContext.GetSystemService(Context.LocationService);

var criteria = new Criteria() { Accuracy = Accuracy.NoRequirement };
bestProvider = _locationManager.GetBestProvider(criteria, true);

Location lastKnownLocation = _locationManager.GetLastKnownLocation(bestProvider);

if (lastKnownLocation != null)
{
string temp = string.Format("Last known location lat: {0}, long: {1}",
lastKnownLocation.Latitude, lastKnownLocation.Longitude);

//string tag = "Last known location: ";

//Log.Info (tag, temp);
}

_locationManager.RequestLocationUpdates(bestProvider, 60000, 20, this);

}
}
}

问题出在行上

locationManager.RequestLocationUpdates (1000, 100, null, locationContext);

参数 4 必须是 Android.App.PendingIntent。我该如何设置它,因为类(class)不是 Activity 。

最佳答案

有一个 RequestLocationUpdates 的重载,它接收一个 LocationListener。参见 The docs .

所以你可以让你的类在 Xamarin 中实现 ILocationListener 并实现所需的方法,例如 onLocationChanged 等。继承自 Java.Lang.Object) 你应该让你的类继承自 Java.Lang.Object 因为它会为你处理 Handle/Disposing。

public class LocationManagerActivity : Java.Lang.Object, ILocationListener

那么你可以这样做

_locationManager.RequestLocationUpdates(bestProvider, 60000, 20, this);

关于android - 设置位置管理器 RequestLocationUpdate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26555356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com