我尝试根据SQLite中设置的时间安排本地通知,我在此链接中使用了某个库:
我已经很好地实施了,但我仍然面临以下问题:
a)只有在我启动应用程序时才会收到通知。
b)所有通知时间都会在SQLite本地存储一次。
现在我该如何让这个应用程序捕获系统时间(电话时间) 并且它甚至在没有启动应用程序的情况下通知警报
我应该使用以下方法之一:
i)OnSleep
ii)OnResume等或......
以下是我将代码放入
的方式App.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using Diabetes.localDB;
using Diabetes.Main;
using Newtonsoft.Json;
using Plugin.Notifications;
using Xamarin.Forms;
namespace Diabetes
{
public partial class App : Application
{
public App()
{
InitializeComponent();
// Setting the Alarm time
MedicationDatabase db = new MedicationDatabase();
//Getting list of time set in SQlite.
var alarm_list = db.GetAlarmList();
// Looping each time to make an alarm
foreach (var alarm in alarm_list)
{
try
{
Debug.WriteLine("Date Time Format : " + alarm);
//This is the library to set Notifications, Date is where the time is attached.
CrossNotifications.Current.Send(new Notification
{
Title = "Hola! ",
Message = "Hey Eddy I remind you take insulin, this is the time you told me to remind you.",
Vibrate = true,
Sound = "pop",
Date = DateTime.Parse(alarm)
});
}
catch (FormatException v)
{
Debug.WriteLine("Format Exception : " + v);
}
catch (OverflowException c)
{
Debug.WriteLine("Overflow Exception : " + c);
}
}
MainPage = new NavigationPage(new Login())
{
BarBackgroundColor = Color.FromHex("#66C8F3"),
BarTextColor = Color.White,
Title = "Diabetics App"
};
}
protected override void OnStart()
{
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
答案 0 :(得分:0)
现在我该如何使这个应用程序捕获系统时间(电话时间),它甚至在没有启动应用程序的情况下通知警报
您可以使用Android.App.AlarmManager,此类提供对系统警报服务的访问。这些允许您安排应用程序在将来的某个时间运行。当警报响起时,系统会广播为其注册的意图,如果目标应用程序尚未运行,则会自动启动它。
由于您正在使用XF进行开发,因此,首先您应该能够在Android客户端项目中创建广播接收器,例如:
[BroadcastReceiver]
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
var message = intent.GetStringExtra("message");
var title = intent.GetStringExtra("title");
var resultIntent = new Intent(context, typeof(MainActivity));
resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
var pending = PendingIntent.GetActivity(context, 0,
resultIntent,
PendingIntentFlags.CancelCurrent);
var builder =
new Notification.Builder(context)
.SetContentTitle(title)
.SetContentText(message)
.SetSmallIcon(Resource.Drawable.Icon)
.SetDefaults(NotificationDefaults.All);
builder.SetContentIntent(pending);
var notification = builder.Build();
var manager = NotificationManager.FromContext(context);
manager.Notify(10010, notification);
}
}
然后在XF中,当我们需要使用PCL中每个平台的本机方法时,我们可以使用DependencyService来实现它。
在PCL中,创建如下界面:
public interface ISetAlarm
{
void SetAlarm(int hour, int minute);
}
然后,在Android客户端项目中,实现此接口,例如:
[assembly: Xamarin.Forms.Dependency(typeof(SetAlarmImplementation))]
namespace YOURNAMESPACE.Droid
{
public class SetAlarmImplementation : ISetAlarm
{
public void SetAlarm(int hour, int minute)
{
Intent myintent = new Intent(Xamarin.Forms.Forms.Context, typeof(AlarmReceiver));
var pendingintent = PendingIntent.GetBroadcast(Xamarin.Forms.Forms.Context, 0, myintent, PendingIntentFlags.UpdateCurrent);
Java.Util.Date date = new Java.Util.Date();
Java.Util.Calendar cal = Java.Util.Calendar.Instance;
cal.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis();
cal.Set(Java.Util.CalendarField.HourOfDay, hour);
cal.Set(Java.Util.CalendarField.Minute, minute);
cal.Set(Java.Util.CalendarField.Second, 0);
AlarmManager alarmManager = Xamarin.Forms.Forms.Context.GetSystemService(Android.Content.Context.AlarmService) as AlarmManager;
alarmManager.Set(AlarmType.RtcWakeup, cal.TimeInMillis, pendingintent);
}
}
}
然后,当你想从PCL调用它时,你可以这样调用它:
DependencyService.Get<ISetAlarm>().SetAlarm(13, 30);