Android 如何使用报警管理器进行通知?

Android 如何使用报警管理器进行通知?,android,database,notifications,alarmmanager,Android,Database,Notifications,Alarmmanager,在我的项目中,我有一个包含年、月、日、小时、分钟的数据库。对于该数据库中的每一行,我希望及时为该行中描述的用户放置带有标题和说明的通知,但当我使用NotificationManager时,当我向数据库添加新时间时,它会立即激活。我读了这篇文章:,关于Alarm Manager示例,但我仍然不明白如何使用它进行通知,因为当我尝试使用它时,什么都没有发生。如果有人能帮我,我会很高兴的。我不清楚你的留言。如果您试图在特定时间启动通知,这是一种方法。使用2项服务;一个服务(您可以称之为SetAlarmS

在我的项目中,我有一个包含年、月、日、小时、分钟的数据库。对于该数据库中的每一行,我希望及时为该行中描述的用户放置带有标题和说明的通知,但当我使用NotificationManager时,当我向数据库添加新时间时,它会立即激活。我读了这篇文章:,关于Alarm Manager示例,但我仍然不明白如何使用它进行通知,因为当我尝试使用它时,什么都没有发生。如果有人能帮我,我会很高兴的。

我不清楚你的留言。如果您试图在特定时间启动通知,这是一种方法。使用2项服务;一个服务(您可以称之为SetAlarmService)用于读取数据库,并使用AlarmManager设置在特定时间启动的挂起意图。您可以通过调用getSystemService(Context.ALARM_SERVICE);,获取实例;。您应该设置您的挂起意图以启动另一个服务(您可以称之为NotifyService),该服务将在启动后立即发出通知

编辑:这里是一个快速示例,请参阅文档以了解参数等的解释

public class AlarmService extends Service {

    Time time;
    AlarmManager alarmMan;

    @Override
    public void onCreate() {
        alarmMan = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        time = new Time();
    }

    @Override
    public int onStartCommand(Intent intent, int startID, int flags) {
        time.setToNow();
        alarmMan.set(AlarmManager.RTC_WAKEUP, time.toMillis(false)+(10*1000), getPIntent());
        time = null;
    }

   public PendingIntent getPIntent() {
        Intent startIntent = new Intent(this, NotifyService.class);
        startIntent.setAction(com.berrmal.remindme.NotifyService.ACTION_SEND_NOTIFICATION);
        PendingIntent pIntent = PendingIntent.getService(this, 0, startIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        return pIntent;
    }

我从一个活动启动此服务,您可以按任何方式执行。NotifyService.class是我编写的另一个服务,它只是立即发布一个粘性通知,我不打算展示它,因为听起来您已经知道如何使用NotificationManager了。这里的关键是10*1000,即警报将在未来激活的毫秒数,以及通知显示的时间。你可以从一个文件中读取,等等。在这个例子中,我只是计算从现在开始的未来10000毫秒。RTC_WAKEUP标志是您想了解的4个标志之一,它们使警报执行稍微不同的操作。希望能有所帮助。

我不清楚你的信息。如果您试图在特定时间启动通知,这是一种方法。使用2项服务;一个服务(您可以称之为SetAlarmService)用于读取数据库,并使用AlarmManager设置在特定时间启动的挂起意图。您可以通过调用getSystemService(Context.ALARM_SERVICE);,获取实例;。您应该设置您的挂起意图以启动另一个服务(您可以称之为NotifyService),该服务将在启动后立即发出通知

编辑:这里是一个快速示例,请参阅文档以了解参数等的解释

public class AlarmService extends Service {

    Time time;
    AlarmManager alarmMan;

    @Override
    public void onCreate() {
        alarmMan = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        time = new Time();
    }

    @Override
    public int onStartCommand(Intent intent, int startID, int flags) {
        time.setToNow();
        alarmMan.set(AlarmManager.RTC_WAKEUP, time.toMillis(false)+(10*1000), getPIntent());
        time = null;
    }

   public PendingIntent getPIntent() {
        Intent startIntent = new Intent(this, NotifyService.class);
        startIntent.setAction(com.berrmal.remindme.NotifyService.ACTION_SEND_NOTIFICATION);
        PendingIntent pIntent = PendingIntent.getService(this, 0, startIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        return pIntent;
    }

我从一个活动启动此服务,您可以按任何方式执行。NotifyService.class是我编写的另一个服务,它只是立即发布一个粘性通知,我不打算展示它,因为听起来您已经知道如何使用NotificationManager了。这里的关键是10*1000,即警报将在未来激活的毫秒数,以及通知显示的时间。你可以从一个文件中读取,等等。在这个例子中,我只是计算从现在开始的未来10000毫秒。RTC_WAKEUP标志是您想了解的4个标志之一,它们使警报执行稍微不同的操作。希望能有所帮助。

我也尝试过,但没有结果。也许我做错了什么,你能给我举个例子吗?我也试过了,没有结果。也许我做错了什么,你能给我举个例子吗?