简体   繁体   English

即使应用未在Android中运行,如何按时间或间隔发送通知?

[英]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.

相关问题 间隔发送一次android通知 - Send a notification with android at an interval 即使应用程序被杀死,如何向Android设备发送推送通知? - How to send push notification to android devices even when app is killed? 即使应用被杀死,如何发送通知 - How to send notification even if app is killed 如何显示通知甚至应用程序未运行 - How to show a notification even app is not running 如何从设备上运行的Android应用程序向另一个运行相同应用程序的设备发送推送通知? - how to send a push notification from an android app running on a device to another device running the same app? 应用程序未运行时如何发送 fcm 通知 - How to send fcm notification when app is not running 如何在Android应用中以正确的时间发送数据? - How do I send data with accurate timing in Android app? 我想显示通知,即使我的Android应用正在运行 - I want to show notification even if my Android app is running 如何安排3个事件通过android中的(通知栏)通知用户,即使我的应用未运行,该事件也会运行 - How to schedule 3 events to notify user via (notification bar) in android which runs even when my app is not running 如何在Android中向应用程序用户发送通知? - How to send notification to app users in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM