如何从 android xamarin 表单在后台 运行 方法 webservice

How to running method webservice in background from android xamarin form

我想要运行他关闭应用程序时使用此方法UpdateStatus

是我在android中的编码方式UpdateStatus:

String id = "";

var id = Application.Current.Properties["Id"].ToString();

User user = new User(id);
user.Id = id;
user.Datetime = time;

var responseStatus = await api.UpdateStatus(new UpdateStatusQuery(user));

你能帮帮我吗?

在Android中,当你关闭你的应用程序时,根据我的研究,上面的代码不能运行,因为当应用程序被杀死时,所有的线程或服务都会被杀死。

如果你想在后台应用运行上面的代码,你可以使用后台服务来实现,因为background execution limits in Android 8.0 or later, if you code need some time to execute, and you want code running stably, Foreground Services是一个不错的选择。

在xamarin forms中,可以在App.xaml.csOnSleep方法中使用dependencyService

OnSleep - 每次应用程序进入后台时调用。

您可以创建一个界面。

IService.cs

public interface IService
{
    void Start();
}

然后实现DependentService启动前台服务

[assembly: Xamarin.Forms.Dependency(typeof(DependentService))]
namespace TabGuesture.Droid
{
[Service]
public class DependentService : Service, IService
{
    public void Start()
    {
        var intent = new Intent(Android.App.Application.Context, 
 typeof(DependentService));


        if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
        {
            Android.App.Application.Context.StartForegroundService(intent);
        }
        else
        {
            Android.App.Application.Context.StartService(intent);
        }
    }

    public override IBinder OnBind(Intent intent)
    {
        return null;
    }
    public const int SERVICE_RUNNING_NOTIFICATION_ID = 10000;
    public override StartCommandResult OnStartCommand(Intent intent, 
    StartCommandFlags flags, int startId)
    {
        // From shared code or in your PCL

        CreateNotificationChannel();
        string messageBody = "service starting";

        var notification = new Notification.Builder(this, "10111")
        .SetContentTitle(Resources.GetString(Resource.String.app_name))
        .SetContentText(messageBody)
        .SetSmallIcon(Resource.Drawable.main)
        .SetOngoing(true)
        .Build();
        StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification);
        //==============================do you work=====================

        String id = "";

        var id = Application.Current.Properties["Id"].ToString();
        User user = new User(id);
        user.Id = id;
        user.Datetime = time;
        var responseStatus = await api.UpdateStatus(new UpdateStatusQuery(user));

        return StartCommandResult.Sticky;
    }


    void CreateNotificationChannel()
    {
        if (Build.VERSION.SdkInt < BuildVersionCodes.O)
        {
            // Notification channels are new in API 26 (and not a part of the
            // support library). There is no need to create a notification
            // channel on older versions of Android.
            return;
        }

        var channelName = Resources.GetString(Resource.String.channel_name);
        var channelDescription = GetString(Resource.String.channel_description);
        var channel = new NotificationChannel("10111", channelName, NotificationImportance.Default)
        {
            Description = channelDescription
        };

        var notificationManager = (NotificationManager)GetSystemService(NotificationService);
        notificationManager.CreateNotificationChannel(channel);
    }
}
}

这是类似的线程: