Android中的简单提醒通知

时间:2012-09-28 22:21:36

标签: android

我正在为Android开发一个简单的提醒列表。当用户按下列表中的提醒时,他会显示标题,描述,日期和时间,以及他在第一次创建提醒时输入的内容。然后,我正在设置alarmManager和broadcastReceiver以根据给定的时间和日期显示提醒通知。

我的问题在于通知。当用户按下通知时,他应该转到该特定提醒屏幕。它仅适用于第一个提醒,问题是当用户创建第二个提醒时,通知的pendingIntent仅显示第一个提醒的屏幕。

我有以下课程:

MainListActivity:显示已创建的提醒列表。 EditActivity:允许用户输入新提醒或编辑现有提醒。 ItemViewActivity:从列表中单击后显示特定的提醒屏幕。 NotifyReceiver:它是一个broadcastReceiver类。 ContentProvider和databaseAdapter类

这是EditActivity的一部分,我在其中创建alarmManager并将pendingIntent设置为broadcastReceiver并向其传递一组用户条目:

Bundle bundle = new Bundle();

    bundle.putString(ToDoDatabaseAdapter.KEY_TITLE, titleText.getText().toString());
    bundle.putString(ToDoDatabaseAdapter.KEY_DATE, dateString);
    bundle.putString(ToDoDatabaseAdapter.KEY_BODY, bodyText.getText().toString());
    bundle.putString(ToDoDatabaseAdapter.KEY_TIME, timeString);

    if (RowId != 0) {
        bundle.putLong(ToDoDatabaseAdapter.KEY_ROWID, RowId);
    }
Intent notify = new Intent(this, Notify.class);
    notify.putExtras(bundle);



PendingIntent contentIntent = PendingIntent.getBroadcast(this, 0, notify, 0);

                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
                cal.set(Calendar.MONTH, datePicker.getMonth());
                cal.set(Calendar.YEAR, datePicker.getYear());
                //cal.set(Calendar.HOUR, timePicker.getCurrentHour());
                cal.set(Calendar.MINUTE, timePicker.getCurrentMinute());
                cal.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
                cal.set(Calendar.SECOND, 0);

                long when = cal.getTimeInMillis();



                AlarmManager alm=(AlarmManager)getSystemService(ALARM_SERVICE);
                alm.set(AlarmManager.RTC_WAKEUP, when, contentIntent );     

这是我的broadcastReceiver类:

@Override
    public void onReceive(Context context, Intent intent) {

        Intent viewIntent = new Intent(context, ItemView.class);
    Bundle bundle = intent.getExtras();

    viewIntent.putExtras(bundle);

    // To get notification alert on the top bar when we press start button:
            int icon = R.drawable.ic_launcher;
            CharSequence tickerText = "Reminder is due";
            long when = System.currentTimeMillis();
            Notification notification = new Notification(icon, tickerText, when);

// this is what we get on the list after pulling down notification window.

            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, viewIntent, 0);
            CharSequence contentTitle = bundle.getString(ToDoDatabaseAdapter.KEY_TITLE);
            CharSequence contentText = bundle.getString(ToDoDatabaseAdapter.KEY_DATE);

            notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

      NotificationManager nm = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
            nm.notify(SERV_DEMO_ID, notification);

            Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            long pattern[] = new long[] {0,200,100,200};
            vibrator.vibrate(pattern,-1);

    }

我玩onResume(),onStart(),onPause(),onStop()方法没有成功。

感谢任何建议。谢谢

0 个答案:

没有答案