简体   繁体   English

Xamarin.Android 中的后台服务

[英]Background Services in Xamarin.Android

I'm struggling to create a background service using Xamarin.Android.我正在努力使用 Xamarin.Android 创建后台服务。 The background service should work every 5 minutes and also should work when the phone's screen off or the application closed.后台服务应该每 5 分钟工作一次,并且在手机屏幕关闭或应用程序关闭时也应该工作。 Do you have any idea how to achieve this.您知道如何实现这一目标吗? I found that library and it's working fine but the problem is interval is not working under 15 minutes.我发现该库工作正常,但问题是间隔在 15 分钟内无法正常工作。 I don't know why.我不知道为什么。

https://www.c-sharpcorner.com/article/scheduling-work-with-workmanager-in-android/ https://www.c-sharpcorner.com/article/scheduling-work-with-workmanager-in-android/

I'm looking forward for your kind supports.我期待着您的支持。 Thank you.谢谢你。

I think BroadcastReceiver will be more appropriate for your task.我认为BroadcastReceiver将更适合您的任务。 It provides more flexibility and setting parameters.它提供了更多的灵活性和设置参数。

Declaring broadcast receiver:声明广播接收器:

using Android.Content;

[BroadcastReceiver]
public class BackgroundBroadcastReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        // Your code here that will be executed periodically
    }
}

Broadcast receiver registration:广播接收器注册:

// context - any of your Android activity

var intentAlarm = new Intent(context, typeof(BackgroundBroadcastReceiver));
var alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService);

alarmManager.SetRepeating(AlarmType.ElapsedRealtime, // Works even application\phone screen goes off
                          DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), // When to start (right now here)
                          1000, // Receiving interval. Set your value here in milliseconds.
                          PendingIntent.GetBroadcast(context, 1, intentAlarm, PendingIntentFlags.UpdateCurrent));

you can use service like bellow code你可以使用像波纹管代码这样的服务

using System;
using System;
using System.Threading;
using Android.App;
using Android.Content;
using Android.OS;
using OomaAndroid.Models;
using SQLite;

namespace OomaAndroid
{
    [Service]
    public class ServiceTest : Service
    {
        Timer timer;        
        public override void OnCreate()
        {
            base.OnCreate();
        }
        public override IBinder OnBind(Intent intent)
        {            
            return null;
        }


        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {            
            timer = new Timer(HandleTimerCallback, 0, 0, 900000);
            return base.OnStartCommand(intent, flags, startId);
        }

        private void HandleTimerCallback(object state)
        {
            //this part codes will run every 15 minutes
        }
    }    
}

also you can run service in the MainActivity您也可以在 MainActivity 中运行服务

      Intent intSer = new Intent(base.ApplicationContext, typeof(OomaService));
      StartService(intSer);

also you should get Receive_Boot_Compeleted permission From user to run service after restarting cellphone您还应该从用户获得 Receive_Boot_Compeleted 权限以在重新启动手机后运行服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM