如何从android xamarin表单在后台运行方法webservice [英] How to running method webservice in background from android xamarin form

查看:64
本文介绍了如何从android xamarin表单在后台运行方法webservice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在他关闭应用程序时运行这个方法 UpdateStatus

I want running this method UpdateStatus when he close app

这是我在android中的编码方法UpdateStatus:

It is my coding method UpdateStatus in android:

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中,当你关闭你的应用程序时,根据我的研究,上面的代码无法运行,因为应用程序时所有的线程或服务都会被杀死被杀了.

In the Android, When you close your applcation, based on my research, above code can not be run, because all of threads or services will be killed when applcation is killed.

如果你想在后台应用程序时运行上面的代码,你可以使用后台服务来实现,由于 Android 8.0 或更高版本中的后台执行限制,如果您的代码需要一些时间来执行,以及您希望代码稳定运行,前台服务 是一个不错的选择.

If you want to run above code when application in the background, you can can use background service to achieve that, due to 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 is a good choice.

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

In xamarin forms, you can use dependenceService in the OnSleep method of App.xaml.cs

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

OnSleep - called each time the application goes to the background.

您可以创建一个界面.

IService.cs

public interface IService
{
    void Start();
}

然后实现DependentService启动前台服务.

Then achieved DependentService to start a Foreground Service.

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

这里是类似的线程:如何创建服务在 Xamarin.Forms 中做一段时间的工作?

这篇关于如何从android xamarin表单在后台运行方法webservice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆