重启后手机设置闹钟 [英] Set alarms after rebooting Phone

查看:229
本文介绍了重启后手机设置闹钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题。我将使用AlarmManager特定的时间我的通知。本次我设置存储在数据库SQLLite通知。他们都工作,我除了重新启动手机的那一刻完美。 alarmManager当然失去其repeatings。

我想问一下什么是在这种情况下,最好的解决办法?我有我的alarmManager在MainActivity设置和设置我的BroadcastReceiver内部通知,您可以在下面的code看到:

下面是我如何把它从MainActivity:

 意向意图=新意图(背景下,MyReceiver.class);
        intent.putExtra(EXTRA_TITLE,职称);
        intent.putExtra(EXTRA_COUNT,计数);
        的PendingIntent的PendingIntent = PendingIntent.getBroadcast(背景下,计数,意图,PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),WEEK_LENGTH_MS,的PendingIntent);

这是广播接收器的方法的onReceive

 公共无效的onReceive(上下文的背景下,意图意图)
    {
        纳米=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        从CharSequence的= context.getString(R.string.app_name);
        CharSequence的消息= intent.getStringExtra(DayActivity.EXTRA_TITLE);
        意图int​​entNotification =新意图(背景下,DayActivity.class);
        的PendingIntent contentIntent = PendingIntent.getActivity(上下文,intent.getIntExtra(DayActivity.EXTRA_COUNT,0),intentNotification,0);
        注意notif =新的通知(R.drawable.notification_logo,context.getString(R.string.app_name),System.currentTimeMillis的());
        notif.setLatestEventInfo(背景下,从日消息,contentIntent);
        notif.defaults | = Notification.DEFAULT_LIGHTS;
        notif.flags | = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
        nm.notify(intent.getIntExtra(DayActivity.EXTRA_COUNT,0),notif);    }

我声明广播接收器的BOOT_COMPLETED事件,但它总是在调用我开始在手机和时间只是一句通知从未以上。


解决方案

  

我想问一下什么是在这种情况下,最好的解决办法?


注册一个 BOOT_COMPLETED 广播接收器调用 setRepeating() AlarmManager 来重新建立你的日程安排。


  

我声明广播接收器的BOOT_COMPLETED事件,但它总是在调用我开始在手机和时间只是一句通知从未以上。


BOOT_COMPLETED的目的 广播接收器应该是重新安排报警。您不妨考虑使用一个单独的广播接收器比您正在使用的报警事件本身的人。

I have a little question. I set my notifications in specific times using AlarmManager. The times I set notifications for are stored in SQLLite database. They all work perfect except the moment I reboot the phone. alarmManager looses their repeatings of course.

I would like to ask what is the best solution in this situation? I have my alarmManager set in MainActivity and I set my notification inside BroadcastReceiver as you can see in the code below:

Here is how I call it from MainActivity:

        Intent intent = new Intent(context, MyReceiver.class);
        intent.putExtra(EXTRA_TITLE, title);
        intent.putExtra(EXTRA_COUNT, count);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, count, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), WEEK_LENGTH_MS, pendingIntent);

And here is Broadcast Receiver's method onReceive

  public void onReceive(Context context, Intent intent)
    {
        nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        CharSequence from = context.getString(R.string.app_name);
        CharSequence message = intent.getStringExtra(DayActivity.EXTRA_TITLE);
        Intent intentNotification = new Intent(context,DayActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, intent.getIntExtra(DayActivity.EXTRA_COUNT,0), intentNotification, 0);
        Notification notif = new Notification(R.drawable.notification_logo,context.getString(R.string.app_name), System.currentTimeMillis());
        notif.setLatestEventInfo(context, from, message, contentIntent);
        notif.defaults |= Notification.DEFAULT_LIGHTS;
        notif.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
        nm.notify(intent.getIntExtra(DayActivity.EXTRA_COUNT,0), notif);

    }

I am declaring BroadcastReceiver for BOOT_COMPLETED event, but it always calls just empty notification at the time i start the phone and never more.

解决方案

I would like to ask what is the best solution in this situation?

Register a BOOT_COMPLETED BroadcastReceiver to call setRepeating() on AlarmManager to re-establish your schedules.

I am declaring BroadcastReceiver for BOOT_COMPLETED event, but it always calls just empty notification at the time i start the phone and never more.

The objective of the BOOT_COMPLETED BroadcastReceiver should be to reschedule your alarms. You might wish to consider using a separate BroadcastReceiver than the one you are using for the alarm events themselves.

这篇关于重启后手机设置闹钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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