如何使用介面

时间:2019-05-06 16:51:32

标签: xamarin interface

我正在尝试构建我的第一个xamarin应用程序,该应用程序是使用表单构建的。该应用程序的功能之一是向用户发送位置信息,即使该应用程序在后台,也必须这样做。因此,我遇到了James Montemagno的GeolocatorPlugin,它承诺会做到这一点。

由于文档尚不清楚如何在后台实现他的插件,因此我仔细研究了项目的关闭问题,并找到了一个家伙,该家伙给出了将插件与服务一起使用的简单案例的示例。 (https://github.com/jamesmontemagno/GeolocatorPlugin/issues/272

我采用了代码并创建了服务。服务正在使用界面来启动服务,现在我的问题是如何利用界面来使服务运行。

在我的共享项目中,我放置了界面和viewmodel,在xamarin.android项目中,我放置了服务。

接口-IGeolocationBackgroundService:

public interface IGeolocationBackgroundService {
    void StartService();
    void StartTracking();
}

视图模型-GeolocatorPageViewModel:

public class GeolocatorPageViewModel
{
    public Position _currentUserPosition { get; set; }
    public string CoordinatesString { get; set; }
    public List<string> userPositions { get; set; }

    public ICommand StartTrackingCommand => new Command(async () =>
    {
        if (CrossGeolocator.Current.IsListening)
        {
            await CrossGeolocator.Current.StopListeningAsync();
        }

        CrossGeolocator.Current.DesiredAccuracy = 25;
        CrossGeolocator.Current.PositionChanged += Geolocator_PositionChanged;

        await CrossGeolocator.Current.StartListeningAsync(
            TimeSpan.FromSeconds(3), 5);
    });

    private void Geolocator_PositionChanged(object sender, PositionEventArgs e)
    {
        var position = e.Position;
        _currentUserPosition = position;
        var positionString = $"Latitude: {position.Latitude}, Longitude: {position.Longitude}";
        CoordinatesString = positionString;
        Device.BeginInvokeOnMainThread(() => CoordinatesString = positionString);
        userPositions.Add(positionString);
        Debug.WriteLine($"Position changed event. User position: {CoordinatesString}");
    }
}

服务-GeolocationService:

[assembly: Xamarin.Forms.Dependency(typeof(GeolocationService))]
namespace MyApp.Droid.Services
{
[Service]
public class GeolocationService : Service, IGeolocationBackgroundService
{
    Context context;

    private static readonly string CHANNEL_ID = "geolocationServiceChannel";
    public GeolocatorPageViewModel ViewModel { get; private set; }

    public override IBinder OnBind(Intent intent)
    {
        return null;
    }

    public GeolocationService(Context context)
    {
        this.context = context;
        CreateNotificationChannel();
    }

    private void CreateNotificationChannel()
    {
        NotificationChannel serviceChannel = new NotificationChannel(CHANNEL_ID,
            "GeolocationService", Android.App.NotificationImportance.Default);
        NotificationManager manager = context.GetSystemService(Context.NotificationService) as NotificationManager;

        manager.CreateNotificationChannel(serviceChannel);

    }

    //[return: GeneratedEnum]
    public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
    {
        var newIntent = new Intent(this, typeof(MainActivity));
        newIntent.AddFlags(ActivityFlags.ClearTop);
        newIntent.AddFlags(ActivityFlags.SingleTop);

        var pendingIntent = PendingIntent.GetActivity(this, 0, newIntent, 0);

        var builder = new Notification.Builder(this, CHANNEL_ID);
        var notification = builder.SetContentIntent(pendingIntent)
            .SetSmallIcon(Resource.Drawable.ic_media_play_light)
            .SetAutoCancel(false)
            .SetTicker("Locator is recording")
            .SetContentTitle("GeolocationService")
            .SetContentText("Geolocator is recording for position changes.")
            .Build();

        StartForeground(112, notification);
        //ViewModel = new GeolocatorPageViewModel();
        return StartCommandResult.Sticky;
    }


    public void StartService()
        => context.StartService(new Intent(context, typeof(GeolocationService)));


    public void StartTracking()
    {
        ViewModel = new GeolocatorPageViewModel();
        ViewModel.StartTrackingCommand.Execute(null);
    }
}
}

请清楚,我需要启动该服务并且不习惯使用接口,那么如何调用该接口?

1 个答案:

答案 0 :(得分:0)

使用DependencyService获取对您的服务的引用,然后启动它

var svc = DependencyService.Get<IGeolocationBackgroundService>();
svc.StartService();
svc.StartTracking();