使用SQLite创建通知

时间:2017-07-04 11:54:51

标签: android database sqlite notifications android-sqlite

我正在尝试开发一款应用,但我遇到了一个问题...... 在我的应用程序中,我有一个数据库,其中包含一些数据,如小时和分钟以及其他一些内容,我想为时间列创建通知。

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

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();
    }

但它不起作用,任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

问题解决了,正如上面提到的PPartisan,问题是PendingIntent。 所以我改变scheduleNotification方法,现在它工作正常: scheduleNotificaion方法:

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);
    }
}

并在onCreate方法中:

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
    }

这只是简单的改动,让我自己明白了...... 感谢PPartisan ......