【问题标题】:Background Service in Xamarin FormsXamarin 表单中的后台服务
【发布时间】:2020-05-22 13:14:02
【问题描述】:

我在android中有一个后台服务实现为:

 [Service]
public class PeriodicService : Service
{
    public override IBinder OnBind(Intent intent)
    {
        return null;
    }

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        base.OnStartCommand(intent, flags, startId);

        // From shared code or in your PCL]
        Task.Run(() => {
            MessagingCenter.Send<string>(this.Class.Name, "SendNoti");
        });

        return StartCommandResult.Sticky;
    }

}

在 MainActivity 类中:

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);
        UserDialogs.Init(() => (Activity)Forms.Context);
        LoadApplication(new App());

        StartService(new Intent(this, typeof(PeriodicService)));
    }
}

在我的登录页面中的 Xamarin 表单中:

 public LoginPage()
    {
        InitializeComponent();

        int i = 0;
        MessagingCenter.Subscribe<string>(this, "SendNoti", (e) =>
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                i++;

                CrossLocalNotifications.Current.Show("Some Text", "This is notification!");                        

                }
            });
        });

    }

这里的主要问题是我的定期服务除了第一次之外没有发送任何消息。通知只显示一次!请帮忙。

【问题讨论】:

  • 你只在你的服务中调用MessagingCenter.Send一次...
  • @SushiHangover 谢谢你的回答。那么如何每 n 小时发送一次通知呢?
  • 通过 AlarmManager/SetRepeating 使用重复警报是安排重复发生事件的更好方法,请在此处查看我的答案:stackoverflow.com/a/45657600/4984832
  • 实际上,这不是警报,而是通知,所以我无法像您在答案中所做的那样发送警报。
  • 您应该阅读未决意图和意图服务,警报管理器非常适合您的用例

标签: android xamarin xamarin.android xamarin.forms


【解决方案1】:

创建一个 IntentService 来发送您的通知:

[Service(Label = "NotificationIntentService")]
public class NotificationIntentService : IntentService
{
    protected override void OnHandleIntent(Intent intent)
    {
        var notification = new Notification.Builder(this)
                           .SetSmallIcon(Android.Resource.Drawable.IcDialogInfo)
                           .SetContentTitle("StackOverflow")
                           .SetContentText("Some text.......")
                           .Build();
        ((NotificationManager)GetSystemService(NotificationService)).Notify((new Random()).Next(), notification);
    }
}

然后使用 AlarmManager 使用“呼叫”您的IntentService 的待定意图设置重复警报:

using (var manager = (Android.App.AlarmManager)GetSystemService(AlarmService))
{
    // Send a Notification in ~60 seconds and then every ~90 seconds after that....
    var alarmIntent = new Intent(this, typeof(NotificationIntentService));
    var pendingIntent = PendingIntent.GetService(this, 0, alarmIntent, PendingIntentFlags.CancelCurrent);
    manager.SetInexactRepeating(AlarmType.RtcWakeup, 1000 * 60, 1000 * 90, pendingIntent);
}

【讨论】:

  • 我确信这可行,但请多多帮助,我如何设置每天上午 10 点、下午 2 点和下午 5 点的通知?此外,如果当时设备已关闭,我需要稍后发送通知。
  • 谢谢,我将此标记为答案,我也找到了我的问题的解决方案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 2020-08-12
  • 1970-01-01
相关资源
最近更新 更多