[英]How to send notification by timing or interval even the app is not running in Android?
I am absolute beginner to Android. 我绝对是Android的初学者。 Now I am about to create a small project on my own. 现在,我将自己创建一个小项目。 That will be a to-do app. 那将是一个待办事项应用程序。 And what I want to do is, user set a task for specific date and time. 我想做的是,用户为特定的日期和时间设置任务。 User may set multiple tasks. 用户可以设置多个任务。 What I want to do is, I want to send notification to user when then time is the same as the time that is set for task even if the app is closed. 我想做的是,即使应用程序已关闭,我希望在该时间与为任务设置的时间相同时向用户发送通知。 How can I achieve that? 我该如何实现? I searched online todo app tutorials. 我搜索了在线待办事项应用程序教程。 But they do not include that feature. 但是它们不包括该功能。
I found this: How to send notification to specific people even if the app is closed? 我发现了这一点: 即使该应用已关闭,如何将通知发送给特定的人? . 。 I think that is not what I want. 我认为那不是我想要的。 I found some. 我找到了 But they are incomplete. 但是它们是不完整的。 So it is very difficult to follow for an absolute beginner. 因此,对于一个绝对的初学者来说很难遵循。
Use Alarm Manager to schedule an event for the specific date and time. 使用警报管理器为特定的日期和时间安排事件。 That will get executed even if your app is in the background by registering broadcast receiver in the manifest file. 即使您的应用程序处于后台,也可以通过在清单文件中注册广播接收器来执行该操作。
Once the broadcast receiver runs, you send a notification 广播接收器运行后,您将发送通知
Hope this gives you the beginning 希望这能给您开始
Create wakeLocker class and call it in broadcastreceiver class. 创建wakeLocker类,并在broadcastreceiver类中调用它。
public abstract class WakeLocker { 公共抽象类WakeLocker {
private static PowerManager.WakeLock wakeLock;
public static void acquire(Context context) {
if (wakeLock != null) wakeLock.release();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "WakeLock");
wakeLock.acquire();
}
public static void release() {
if (wakeLock != null) wakeLock.release(); wakeLock = null;
}
more more details, refer http://developer.android.com/reference/android/os/PowerManager.WakeLock.html ? 更多详细信息,请参阅http://developer.android.com/reference/android/os/PowerManager.WakeLock.html ?
Call the method (acquire) like this. 像这样调用方法(获取)。
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
WakeLocker.acquire(context);
// your code
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.