从OnDestroy或OnPause中的日历获取数据

时间:2017-10-09 10:34:52

标签: android xamarin calendar onpause ondestroy

我有这段代码:

namespace TestB
{
    [Activity(Label = "TestB", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        Java.Util.Calendar calendar;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            Button button = FindViewById<Button>(Resource.Id.MyButton);

            try
            {
                calendar = Java.Util.Calendar.Instance;
                calendar.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis();
            }
            catch (Exception ex)
            {
                Toast.MakeText(this, ex.Message.ToString(), ToastLength.Short).Show();
            }
        }
        protected override void OnDestroy()
        {
            base.OnDestroy();
            if (calendar.Get(Java.Util.CalendarField.HourOfDay) == 13 && calendar.Get(Java.Util.CalendarField.Minute) == 33)
                Toast.MakeText(this, calendar.Get(Java.Util.CalendarField.HourOfDay).ToString() + " " + calendar.Get(Java.Util.CalendarField.Minute).ToString(), ToastLength.Short).Show();
        }
        protected override void OnPause()
        {
            base.OnPause();
            if (calendar.Get(Java.Util.CalendarField.HourOfDay) == 13 && calendar.Get(Java.Util.CalendarField.Minute) == 33)
                Toast.MakeText(this, calendar.Get(Java.Util.CalendarField.HourOfDay).ToString() + " " + calendar.Get(Java.Util.CalendarField.Minute).ToString(), ToastLength.Short).Show();
        }

当时间到达13和33分钟时,将不会发出消息。 我想以这种方式调用alarmManager。 我希望calendar.Set(Java.Util.CalendarField.HourOfDay, 13)calendar.Set(Java.Util.CalendarField.Minute, 33)并以这种方式致电alarmManager

编辑1:

我编辑了这段代码:

System.Timers.Timer t;
                if (t == null)
                {
                    t = new System.Timers.Timer();
                    //t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
                    //t.Elapsed += (sender, args) => FireTheMissiles();
                    t.Elapsed += new System.Timers.ElapsedEventHandler(OnTimeEvent);
                    t.Interval = 1000;
                    t.Enabled = true;
                }
                t.Start();
        protected override void OnStop()
        {
            base.OnStop();
            flag = 3;
        }
        private void OnTimeEvent(object source, ElapsedEventArgs e)
        {
            h = DateTime.Now.Hour;
            m = DateTime.Now.Minute;

            RunOnUiThread(delegate
            {
                if (flag == 3)
                {
                    if (calendar.Get(Java.Util.CalendarField.HourOfDay) == h && calendar.Get(Java.Util.CalendarField.Minute) == m)
                        Toast.MakeText(this, "3 " + calendar.Get(Java.Util.CalendarField.HourOfDay).ToString() + " " + calendar.Get(Java.Util.CalendarField.Minute).ToString(), ToastLength.Short).Show();
                }
            });
        }

我让我的朋友在另一个应用程序上实现这个项目,看看如果应用程序24小时没有打开就会出现一条消息,但是不会出现任何消​​息。

编辑2:

我用了你的代码 我也用过计时器 我将安装的时间和日期等信息保存在数据库中 我希望闹钟按时间和日期显示 但经过多次尝试,我没有收到任何警告信息。

private void OnTimeEvent(object source, ElapsedEventArgs e)
{
    RunOnUiThread(delegate
    {
        if (flag == 1)
        {
        }
        int h = DateTime.Now.Hour;
        int m = DateTime.Now.Minute;
        DateTime today = DateTime.Today;
        dc = today.AddDays(1).Day;
        mc = today.AddDays(1).Month;
        yc = today.AddDays(1).Year;
        var data = mydb.Table<DatB>();
        var data1 = data.Where(x => x.DWSeen == dc).Where(x => x.MWSeen == mc).FirstOrDefault();
        if (flag == 3 && data1 != null && data1.RFSeen == 0)
        {
            startAlertAtParticularTime(data1.HourSeen, data1.MinuteSeen, "-1");
            if (calendar.Get(Java.Util.CalendarField.HourOfDay) == h && calendar.Get(Java.Util.CalendarField.Minute) == m && calendar.Get(Java.Util.CalendarField.DayOfMonth) == dc && calendar.Get(Java.Util.CalendarField.Month) == mc && calendar.Get(Java.Util.CalendarField.Year) == yc && data1.RFSeen == 0)
            {
                startAlertAtParticularTime(data1.HourSeen, data1.MinuteSeen, data1.RFSeen.ToString());
            }
        }
    });
}

&#34; -1&#34;表示警报不会出现 &#34; 0&#34;表示将出现警报

namespace TestB
{
    [BroadcastReceiver(Enabled = true)]
    public class MyBR: BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            string dataSeenResult = intent.GetStringExtra("dataSeen");
            if ((MyApp.isActivityVisible() == "Stopped" || MyApp.isActivityVisible() == "Destroyed") && dataSeenResult == "0")
            {
                Toast.MakeText(context, MyApp.isActivityVisible().ToString(), ToastLength.Short).Show();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

  

是的我希望应用程序在关闭时的特定时间发出警报

首先,您需要创建一个BroadcastReceiver来处理来自系统的警报通知。然后,您可以使用AlarmManagerOnPauseOnDestroy中设置闹钟:

Intent myintent = new Intent(this, typeof(AlarmReceiver));
var pendingintent = PendingIntent.GetBroadcast(this, 0, myintent, PendingIntentFlags.UpdateCurrent);

Java.Util.Date date = new Java.Util.Date();
Java.Util.Calendar cal = Java.Util.Calendar.Instance;
calendar.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis();
cal.Set(Java.Util.CalendarField.HourOfDay, 13);
cal.Set(Java.Util.CalendarField.Minute, 33);
cal.Set(Java.Util.CalendarField.Second, 0);
AlarmManager alarmManager = this.GetSystemService(AlarmService) as AlarmManager;
alarmManager.Set(AlarmType.RtcWakeup, cal.TimeInMillis, pendingintent);

对于BroadcastReceiver部分,在这种情况下,您可以参考Cheesebaron的答案:Xamarin AlarmManager Android