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

时间:2014-02-14 11:44:42

标签: 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);

    //...
}

直到现在我正在计算当前时间和存储时间之间的差异时间,并让警报管理员获取通知......但我需要根据时间和日期存储在数据库中的所有值。

1 个答案:

答案 0 :(得分:1)

您需要在

中使用不同的ID设置闹钟
  

警报接收器

类,这里发生的事情是同一个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());