Android 使用SQLite创建通知

Android 使用SQLite创建通知,android,database,sqlite,notifications,android-sqlite,Android,Database,Sqlite,Notifications,Android Sqlite,我正在尝试开发一个应用程序,但我遇到了一个问题。。。 在我的应用程序中,我有一个包含小时和分钟等数据的数据库,我想为时间列创建一个通知 我使用以下代码创建了通知: public void scheduleNotification(Notification notification, int hour, int minute) { Intent notificationIntent = new Intent(getContext().getApplicationContext(), Tas

我正在尝试开发一个应用程序,但我遇到了一个问题。。。 在我的应用程序中,我有一个包含小时和分钟等数据的数据库,我想为时间列创建一个通知

我使用以下代码创建了通知:

public void scheduleNotification(Notification notification, int hour, int minute) {
    Intent notificationIntent = new Intent(getContext().getApplicationContext(), TaskReceiver.class);
    notificationIntent.putExtra(TaskReceiver.NOTIFICATION_ID, 0);
    notificationIntent.putExtra(TaskReceiver.NOTIFICATION, notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext().getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, 0);
    AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}

private Notification getNotification(String content) {
    Notification.Builder builder = new Notification.Builder(getContext());
    builder.setContentTitle("Text Title");
    builder.setContentText("Some Text");
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setAutoCancel(true);
    builder.setVibrate(new long[]{1000, 1000, 1000});

    Intent intent = new Intent(getContext(), MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 1, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
    builder.setContentIntent(pendingIntent);
    return builder.build();
}
并在活动中调用它,如:

Cursor c = dbManager.fetch();
    if(c != null){
        while (c.moveToNext()){
            int hour = c.getInt(c.getColumnIndex(DatabaseHelper.CLOCK_HOUR));
            int minute = c.getInt(c.getColumnIndex(DatabaseHelper.CLOCK_MINUTE));

            scheduleNotification(getNotification("Test"), hour, minute);
        }
    }else {
        Toast.makeText(getContext(), "Cursor is empty", Toast.LENGTH_SHORT).show();
    }

但它不起作用,有人能帮我吗?

问题解决了,正如帕蒂桑上面提到的,问题悬而未决。 所以我改变了scheduleNotification方法,现在它可以正常工作了: 计划通知方法:

public void scheduleNotification(int hour , int minute){
    Random random = new Random();
    int randomNumber = random.nextInt(500) + 20;

    Calendar calendar = Calendar.getInstance();
    int curHour = calendar.get(Calendar.HOUR_OF_DAY);
    int curMinute = calendar.get(Calendar.MINUTE);

    int subHour = hour - curHour;
    int subMinute = minute - curMinute;

    if(subHour < 0){
        Toast.makeText(this, "Alarm hour Passed...", Toast.LENGTH_SHORT).show();
    } else if(subMinute < 0){
        Toast.makeText(this, "Alarm minute passed...", Toast.LENGTH_SHORT).show();
    } else {
        Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), randomNumber, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + subHour * 60 * 60 * 1000 + subMinute * 60 * 1000, pendingIntent);
    }
}
这只是简单的改变,让我明白了。。。
多亏了PPartisan…

试试这个方法@AniruddhParihar它对我不起作用,因为当我从数据库读取数据并将它们设置为通知时,通知不会显示。我不知道为什么.你说的“不工作”是什么意思?是否看到错误消息,是否尝试过任何调试?如果是,结果是什么?@PPartisan是的,我做了所有事情,我的意思是通知只显示在我的listView中的最后一项。我无法访问我数据库中的所有记录“我无法访问我数据库中的所有记录”-你的意思是你的数据库没有返回预期的行数?很高兴我的解决方案有所帮助,但仅供参考,你当前生成唯一ID的系统不能保证你的ID实际上是唯一的。使用SQLiteDatabase的
\u id
列中存储的值将非常健壮。或者,您可以创建一个使用
AtomicInteger
的中央计数器(请参阅
视图的源代码#generateViewId
或有关如何执行此操作的想法)…尽管这不会在重新启动应用程序后继续存在。
if (listView.getAdapter().getCount() != 0){
        emptyText.setVisibility(View.GONE);
        cursor.moveToFirst();
        int hour = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.HOUR));
        int minute = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.MINUTE));
        scheduleNotification(hour, minute);
        while (cursor.moveToNext()){
            int hourNext = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.HOUR));
            int minuteNext = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.MINUTE));
            scheduleNotification(hourNext, minuteNext);
        }
    } else {
        //what ever
    }