即使应用程序关闭,如何每天在特定时间显示通知?

编程入门 行业动态 更新时间:2024-10-15 10:21:15
本文介绍了即使应用程序关闭,如何每天在特定时间显示通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

虽然之前在 Stack Overflow 上可能有人问过这个问题,但我仍然没有找到明确的答案.

Although this question might have been asked before on Stack Overflow, I still haven't found a clear answer.

我想在每天中午 12 点显示通知,例如,即使应用程序已关闭.我从这些链接中引用了:每天特定时间的通知 android, Android每日特定时间重复通知使用 AlarmManager 的一天,启动时的 Android BroadcastReceiver - 当 Activity 在后台时保持运行a> 以及更多...我对 Service 和 BroadcastReceiver 之间的区别感到困惑.我该用哪一种?还是我应该同时使用它们?

I want to show a notification everyday at 12pm for example even when the app is closed. I have referenced from those links: Notifications in specific time every day android, Android daily repeating notification at specific time of a day using AlarmManager, Android BroadcastReceiver on startup - keep running when Activity is in Background and much more... I'm confused on the difference between Service and BroadcastReceiver. Which one shall I use? or shall I use both of them?

到目前为止,我知道如何显示通知,但我不知道如何在应用程序关闭的情况下每天自动显示一次.

So far, I know how to show a notification, but I don't know how to show it automatically once everyday even when the app is closed.

我的代码:

public class NotifyService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Toast.makeText(this, "Service created", Toast.LENGTH_LONG).show(); Intent resultIntent = new Intent(this, HomeScreen.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0); Notification.Builder notification = new Notification.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("App Title") .setContentText("Some Text...") .setContentIntent(resultPendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT < 16) { notificationManager.notify(1, notification.getNotification()); } else { notificationManager.notify(1, notification.build()); } } @Override public void onDestroy() { super.onDestroy(); Toast.makeText(this, "Service destroyed", Toast.LENGTH_LONG).show(); } }

AppManifest.xml:

<service android:name=".NotifyService" />

我应该如何编写代码来完成我想要的?任何我可以理解的建议或任何好的链接?

How should I write my code to accomplish what I want? Any suggestions or any good link that I can understand from?

推荐答案

如果我理解正确的话,我相信您需要使用 AlarmManager 设置一个循环警报.您还需要设置在设备重启时启动警报服务.您可以编写一个方法来执行您想要的操作,以便在警报运行时执行它,例如显示通知.以下链接应该对您有所帮助:

If I understood you correctly, I believe that you need setup a recurring alarm using AlarmManager. You also need to setup starting alarm service on device reboot. You can write a method that does what you want so it get executed when the alarm runs e.g. show notification. The following links should help you:

  • Android 基础:安排重复性任务
  • 使用 AlarmManager 在 Android 中重复报警示例

更多推荐

即使应用程序关闭,如何每天在特定时间显示通知?

本文发布于:2023-11-27 13:35:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638295.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   通知   时间   在特定

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!