android - 如何从数据库中获取时间值并设置警报以生成通知

标签 android database

我有一个应用程序可以从数据库中获取时间和日期值,并且应该将其提供给警报以生成通知...但是我不知道如何从数据库中获取日期和时间值并将其提供给警报管理器以获取通知...我在数据库中有很多时间和日期...我必须获取时间和日期并将其提供给警报管理器以生成通知...到目前为止我已经尝试过仅在一天内生成的代码...当我发出新警报时,旧警报会被删除...只有最后一次设置的警报时间会提供给警报管理器,并且它只会向最后一组发出通知...请帮帮我...`

Calendar ca = Calendar.getInstance();
ca.setTimeInMillis(System.currentTimeMillis());
int hr = ca.get(Calendar.HOUR)*60*60*1000;
int hr1 = ca.get(Calendar.MINUTE)*60*1000;
int hr2 = ca.get(Calendar.SECOND)*1000;

int cal = hr + hr1;

for(int y=1;y<h.length;y++){
    Toast.makeText(getApplicationContext(), "" +h[y],3000).show();
    String q[]=h[y].split(":");

    Integer i=Integer.parseInt(q[0]) * 60 * 60 * 1000;
    Integer i2=Integer.parseInt(q[1]) * 60 * 1000;

    long set= i + i2;
    long interval= set - cal;
    Toast.makeText(getApplicationContext(), "" +interval,3000).show();
    intr.add(interval);

    y++;
}

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();

for(int i = 0; i < intr.size(); ++i){
    Intent intentAlarm = new Intent(getApplicationContext(),AlarmReciever.class);
    PendingIntent p=PendingIntent.getBroadcast(getApplicationContext(), i, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.set(AlarmManager.RTC_WAKEUP, ca.getTimeInMillis()+intr.get(i), p);

    //...
}

到目前为止,我正在计算当前时间和存储时间之间的时间差,并提供给警报管理器以获取通知......但我需要根据时间和日期存储在数据库中的所有值......

最佳答案

您需要在您的

中设置具有不同 ID 的闹钟

Alarm Receiver

类,这里发生的事情是将相同的 ID 替换为旧的 ID,因此只会调用最新的警报。请根据您的警报接收器类中的新警报更改 ID。

我是这样实现的

闹钟设置

   AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(getBaseContext(),
                    AlarmReceiver1.class);

            intent.putExtra("id", i);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(
                    context, i, intent, PendingIntent.FLAG_ONE_SHOT);
            alarmManager.set(AlarmManager.RTC_WAKEUP,
                    calSet.getTimeInMillis(), pendingIntent);

在接收类中

//先获取ID

 id = intent.getIntExtra("id", 0);

//设置闹钟

 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                context)
                .setSmallIcon(com.card2contacts.R.drawable.appicon)
                .setContentTitle("Follow up with ")
                .setContentText("")
                .setSound(
                        RingtoneManager
                                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setDefaults(Notification.DEFAULT_VIBRATE)
                .setDefaults(Notification.FLAG_AUTO_CANCEL);
        Intent resultIntent = new Intent(context, FollowUp.class);
        // The stack builder object will contain an artificial back stack
        // for the started Activity.
        // This ensures that navigating backward from the Activity leads out
        // of your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(FollowUp.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        // Id allows you to update the notification later on.

        mNotificationManager.notify(id, mBuilder.build());

关于android - 如何从数据库中获取时间值并设置警报以生成通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21778509/

相关文章:

android - Retrofit:是否可以传递 bean 并获得表单编码?

android - 从 RGB 代码中获取颜色名称?

node.js - 如何在如下所示的聚合查询中添加排序、限制和跳过?

c++ - stx-btree 存储在磁盘上

android - ARC 蓝牙支持

android - 需要帮助来绘制视频流的位图

android - 用户不活动时屏幕变暗

sql - 链接到数据库的 MS Access 前端中的架构更改未更新

mysql - 自动更新另一个表中的出现次数

javascript - 从 FIrebase 实时数据库获取数据