每天使用AlarmManager和服务显示通知

尼古拉斯

我想每天显示一个通知,但是该通知会不时显示。到目前为止,我还没有弄清楚这种模式。

在我中,MainActivity#onCreate我执行以下代码来启动它:

final Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.add(Calendar.DAY_OF_YEAR, 1);

final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, getPendingIntentForDailyReminderService(context));

为了停止AlarmManager,我有以下代码(仅在用户更改首选项时执行):

final AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.cancel(getPendingIntentForDailyReminderService(context));

该函数getPendingIntentForDailyReminderService定义如下:

final Intent intent = new Intent(context, DailyReminderService.class);
return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

这是我的服务班级:

public class DailyReminderService extends Service {
    private static final int NOTIFICATION_ID = 1;

    @Override
    public IBinder onBind(final Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(final Intent intent, final int flags, final int startId) {
        final String contentText = this.getString(R.string.daily_reminder_text);

        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle(this.getString(R.string.app_name));
        builder.setContentText(contentText);
        builder.setSmallIcon(R.drawable.ic_notification_icon);
        builder.setStyle(new NotificationCompat.BigTextStyle().bigText(contentText));

        final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);

        final Notification notification = builder.build();
        notification.flags = Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;

        final NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, notification);

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        final NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancel(NOTIFICATION_ID);

        super.onDestroy();
    }
}

另外,我已经在清单中注册了该服务:

<service
    android:name=".dailyreminder.DailyReminderService"
    android:enabled="true"
    android:exported="true">

我究竟做错了什么?

ach

正确的方法是使用BroadcastReceiver而不是Service

如果您START_STICKYonStartCommand服务返回并且从未明确停止服务,则每次由于资源不足而终止该服务时,操作系统都会在有资源的情况下尝试在稍后时间重新启动该服务。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Google API OAuth 2.0服务帐户C#首次使用与每天使用

来自分类Dev

每天使用cron 12AM自动重新启动apache服务

来自分类Dev

每天使用Selenium下载不同的文件

来自分类Dev

每天使用Linq分组最早的条目

来自分类Dev

计算每天使用的不同 ID 的数量

来自分类Dev

每天使用窗口函数的id计数

来自分类Dev

每天使用Rails自动创建模型实例

来自分类Dev

每天使用jquery或php弹出一次

来自分类Dev

每天使用 logrotate 擦除干净的日志文件

来自分类Dev

如何每天使用 aws 复制生产数据库

来自分类Dev

使用AlarmManager运行通知

来自分类Dev

Android如何使用AlarmManager和服务类安排另一个活动的重复方法

来自分类Dev

如何设置AlarmManager对于每天和每天都要重复多次的通知

来自分类Dev

使用onHandleIntent的AsyncTask和服务?

来自分类Dev

如何使用目标 c 中的本地通知每天在特定时间调用 Web 服务?

来自分类Dev

每天使用log4j创建新的日志文件

来自分类Dev

Fullcalendar,每天使用数量限制按钮需要查看更多/更多按钮的文件吗?

来自分类Dev

每天使用另一个表中的值更新sql表

来自分类Dev

如何在弹性搜索中每天使用Serilog自动生成索引

来自分类Dev

每天使用什么存储设备写入10GB的临时数据?

来自分类Dev

每天使用cron运行脚本并创建压缩的日志文件

来自分类Dev

允许用户每天使用Codeigniter提交3次数据

来自分类Dev

每天使用不同的jenkins构建文件夹

来自分类Dev

Python - 每天使用 Schedule 从数据库中同时填充数据

来自分类Dev

使用BroadCastreceiver和服务打开设备wi-Fi时如何显示Toast?

来自分类Dev

我选择了随机通知天,但每天都会显示通知

来自分类Dev

如何根据天使用javascript或php隐藏/显示div?

来自分类Dev

Laravel - 仅显示今天使用 carbon 创建的帖子

来自分类Dev

如何使用 AlarmManager 停止服务

Related 相关文章

  1. 1

    Google API OAuth 2.0服务帐户C#首次使用与每天使用

  2. 2

    每天使用cron 12AM自动重新启动apache服务

  3. 3

    每天使用Selenium下载不同的文件

  4. 4

    每天使用Linq分组最早的条目

  5. 5

    计算每天使用的不同 ID 的数量

  6. 6

    每天使用窗口函数的id计数

  7. 7

    每天使用Rails自动创建模型实例

  8. 8

    每天使用jquery或php弹出一次

  9. 9

    每天使用 logrotate 擦除干净的日志文件

  10. 10

    如何每天使用 aws 复制生产数据库

  11. 11

    使用AlarmManager运行通知

  12. 12

    Android如何使用AlarmManager和服务类安排另一个活动的重复方法

  13. 13

    如何设置AlarmManager对于每天和每天都要重复多次的通知

  14. 14

    使用onHandleIntent的AsyncTask和服务?

  15. 15

    如何使用目标 c 中的本地通知每天在特定时间调用 Web 服务?

  16. 16

    每天使用log4j创建新的日志文件

  17. 17

    Fullcalendar,每天使用数量限制按钮需要查看更多/更多按钮的文件吗?

  18. 18

    每天使用另一个表中的值更新sql表

  19. 19

    如何在弹性搜索中每天使用Serilog自动生成索引

  20. 20

    每天使用什么存储设备写入10GB的临时数据?

  21. 21

    每天使用cron运行脚本并创建压缩的日志文件

  22. 22

    允许用户每天使用Codeigniter提交3次数据

  23. 23

    每天使用不同的jenkins构建文件夹

  24. 24

    Python - 每天使用 Schedule 从数据库中同时填充数据

  25. 25

    使用BroadCastreceiver和服务打开设备wi-Fi时如何显示Toast?

  26. 26

    我选择了随机通知天,但每天都会显示通知

  27. 27

    如何根据天使用javascript或php隐藏/显示div?

  28. 28

    Laravel - 仅显示今天使用 carbon 创建的帖子

  29. 29

    如何使用 AlarmManager 停止服务

热门标签

归档