-1

I have created a Reminder App with Snoozing option.. Everything is alright when snoozing is activated for only one reminder.. When i add a second reminder, the first one was duplicated(overide) by the second one.. I attched the code here. Plz help me..

reminButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            contentTitle = nameEdit.getText().toString();
            contentText = descEdit.getText().toString();
            contentSnooze = snoozeEdit.getText().toString();

            ContentValues details = new ContentValues();
            details.put("Name", contentTitle);
            details.put("Description", contentText);
            details.put("SnoozeTime", contentSnooze);

            db.insert("snoozetable", null, details);

            Toast.makeText(getApplication(), "Successfully Saved", Toast.LENGTH_LONG).show();

            if (dateFlag == true && timeFlag == true) {
                notificationCount = notificationCount + 1;
                dateFlag = false;
                timeFlag = false;
                time = mYear + "-" + mMonth + "-" + mDay + " " + mHour
                        + "-" + mMinute;
                SimpleDateFormat df = new SimpleDateFormat(
                        "yyyy-MM-dd hh-mm");
                Date dt = null;
                try {
                    dt = df.parse(time);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (java.text.ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                long when = dt.getTime();
                /*contentTitle = nameEdit.getText().toString();
                contentText = descEdit.getText().toString();*/
                AlarmManager mgr = (AlarmManager) mContext
                        .getSystemService(Context.ALARM_SERVICE);
                Intent notificationIntent = new Intent(mContext,
                        ReminderAlarm.class);
                notificationIntent.putExtra("Name", contentTitle);
                notificationIntent.putExtra("Description", contentText);
                notificationIntent.putExtra("NotifyCount",
                        notificationCount);
                PendingIntent pi = PendingIntent.getBroadcast(mContext,
                        notificationCount, notificationIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                mgr.set(AlarmManager.RTC_WAKEUP, when, pi);
                Toast.makeText(mContext, contentTitle +" Reminder Activated" + notificationCount,
                        Toast.LENGTH_LONG).show();
                contentTitle = "";
                contentText = "";
                descEdit.setText("");
                snoozeEdit.setText("");

            }

reminButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            contentTitle = nameEdit.getText().toString();
            contentText = descEdit.getText().toString();
            contentSnooze = snoozeEdit.getText().toString();

            ContentValues details = new ContentValues();
            details.put("Name", contentTitle);
            details.put("Description", contentText);
            details.put("SnoozeTime", contentSnooze);

            db.insert("snoozetable", null, details);

            Toast.makeText(getApplication(), "Successfully Saved", Toast.LENGTH_LONG).show();

            if (dateFlag == true && timeFlag == true) {
                notificationCount = notificationCount + 1;
                dateFlag = false;
                timeFlag = false;
                time = mYear + "-" + mMonth + "-" + mDay + " " + mHour
                        + "-" + mMinute;
                SimpleDateFormat df = new SimpleDateFormat(
                        "yyyy-MM-dd hh-mm");
                Date dt = null;
                try {
                    dt = df.parse(time);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (java.text.ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                long when = dt.getTime();
                /*contentTitle = nameEdit.getText().toString();
                contentText = descEdit.getText().toString();*/
                AlarmManager mgr = (AlarmManager) mContext
                        .getSystemService(Context.ALARM_SERVICE);
                Intent notificationIntent = new Intent(mContext,
                        ReminderAlarm.class);
                notificationIntent.putExtra("Name", contentTitle);
                notificationIntent.putExtra("Description", contentText);
                notificationIntent.putExtra("NotifyCount",
                        notificationCount);
                PendingIntent pi = PendingIntent.getBroadcast(mContext,
                        notificationCount, notificationIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                mgr.set(AlarmManager.RTC_WAKEUP, when, pi);
                Toast.makeText(mContext, contentTitle +" Reminder Activated" + notificationCount,
                        Toast.LENGTH_LONG).show();
                contentTitle = "";
                contentText = "";
                descEdit.setText("");
                snoozeEdit.setText("");

            }
reminButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            contentTitle = nameEdit.getText().toString();
            contentText = descEdit.getText().toString();
            contentSnooze = snoozeEdit.getText().toString();

            ContentValues details = new ContentValues();
            details.put("Name", contentTitle);
            details.put("Description", contentText);
            details.put("SnoozeTime", contentSnooze);

            db.insert("snoozetable", null, details);

            Toast.makeText(getApplication(), "Successfully Saved", Toast.LENGTH_LONG).show();

            if (dateFlag == true && timeFlag == true) {
                notificationCount = notificationCount + 1;
                dateFlag = false;
                timeFlag = false;
                time = mYear + "-" + mMonth + "-" + mDay + " " + mHour
                        + "-" + mMinute;
                SimpleDateFormat df = new SimpleDateFormat(
                        "yyyy-MM-dd hh-mm");
                Date dt = null;
                try {
                    dt = df.parse(time);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (java.text.ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                long when = dt.getTime();
                /*contentTitle = nameEdit.getText().toString();
                contentText = descEdit.getText().toString();*/
                AlarmManager mgr = (AlarmManager) mContext
                        .getSystemService(Context.ALARM_SERVICE);
                Intent notificationIntent = new Intent(mContext,
                        ReminderAlarm.class);
                notificationIntent.putExtra("Name", contentTitle);
                notificationIntent.putExtra("Description", contentText);
                notificationIntent.putExtra("NotifyCount",
                        notificationCount);
                PendingIntent pi = PendingIntent.getBroadcast(mContext,
                        notificationCount, notificationIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                mgr.set(AlarmManager.RTC_WAKEUP, when, pi);
                Toast.makeText(mContext, contentTitle +" Reminder Activated" + notificationCount,
                        Toast.LENGTH_LONG).show();
                contentTitle = "";
                contentText = "";
                descEdit.setText("");
                snoozeEdit.setText("");

            }

reminButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            contentTitle = nameEdit.getText().toString();
            contentText = descEdit.getText().toString();
            contentSnooze = snoozeEdit.getText().toString();

            ContentValues details = new ContentValues();
            details.put("Name", contentTitle);
            details.put("Description", contentText);
            details.put("SnoozeTime", contentSnooze);

            db.insert("snoozetable", null, details);

            Toast.makeText(getApplication(), "Successfully Saved", Toast.LENGTH_LONG).show();

            if (dateFlag == true && timeFlag == true) {
                notificationCount = notificationCount + 1;
                dateFlag = false;
                timeFlag = false;
                time = mYear + "-" + mMonth + "-" + mDay + " " + mHour
                        + "-" + mMinute;
                SimpleDateFormat df = new SimpleDateFormat(
                        "yyyy-MM-dd hh-mm");
                Date dt = null;
                try {
                    dt = df.parse(time);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (java.text.ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                long when = dt.getTime();
                /*contentTitle = nameEdit.getText().toString();
                contentText = descEdit.getText().toString();*/
                AlarmManager mgr = (AlarmManager) mContext
                        .getSystemService(Context.ALARM_SERVICE);
                Intent notificationIntent = new Intent(mContext,
                        ReminderAlarm.class);
                notificationIntent.putExtra("Name", contentTitle);
                notificationIntent.putExtra("Description", contentText);
                notificationIntent.putExtra("NotifyCount",
                        notificationCount);
                PendingIntent pi = PendingIntent.getBroadcast(mContext,
                        notificationCount, notificationIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                mgr.set(AlarmManager.RTC_WAKEUP, when, pi);
                Toast.makeText(mContext, contentTitle +" Reminder Activated" + notificationCount,
                        Toast.LENGTH_LONG).show();
                contentTitle = "";
                contentText = "";
                descEdit.setText("");
                snoozeEdit.setText("");

            }

ReminderActivity.java

reminButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            contentTitle = nameEdit.getText().toString();
            contentText = descEdit.getText().toString();
            contentSnooze = snoozeEdit.getText().toString();


                time = mYear + "-" + mMonth + "-" + mDay + " " + mHour
                        + "-" + mMinute;
                SimpleDateFormat df = new SimpleDateFormat(
                        "yyyy-MM-dd hh-mm");
                Date dt = null;
                try {
                    dt = df.parse(time);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (java.text.ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                long when = dt.getTime();

                AlarmManager mgr = (AlarmManager) mContext
                        .getSystemService(Context.ALARM_SERVICE);
                Intent notificationIntent = new Intent(mContext,
                        ReminderAlarm.class);
                notificationIntent.putExtra("Name", contentTitle);
                notificationIntent.putExtra("Description", contentText);
                notificationIntent.putExtra("NotifyCount",
                        notificationCount);
                PendingIntent pi = PendingIntent.getBroadcast(mContext,
                        notificationCount, notificationIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                mgr.set(AlarmManager.RTC_WAKEUP, when, pi);
                Toast.makeText(mContext, contentTitle +" Reminder Activated" + notificationCount,
                        Toast.LENGTH_LONG).show();




        }

ReminderAlarm.java

public void onReceive(Context context, Intent intent) {

    mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    CharSequence name = intent.getStringExtra("Name");
    CharSequence desc = intent.getStringExtra("Description");
    notificationCount = Integer.parseInt(intent.getExtras().get("NotifyCount")
            .toString());

    sound = new Sound(context);
    sound.play();

    Intent notifyIntent = new Intent(context, ReminderPopup.class);
    notifyIntent.putExtra("Name", name);
    notifyIntent.putExtra("Description", desc);
    notifyIntent.putExtra("NotifyCount", notificationCount);
    PendingIntent contentIntent = PendingIntent.getActivity(context, notificationCount,
            notifyIntent, 0);

    notification = new Notification(R.drawable.ic_launcher, "Notification",
            System.currentTimeMillis());
    notification.setLatestEventInfo(context, name, desc, contentIntent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    mNotificationManager.notify(
            Integer.parseInt(intent.getExtras().get("NotifyCount")
                    .toString()), notification);

ReminderPopup.java

public void onClick(DialogInterface dialog, int which) {
            if (minute != 0) {



                cal = Calendar.getInstance();
                mYear = cal.get(Calendar.YEAR);
                mMonth = cal.get(Calendar.MONTH) + 1;
                mDay = cal.get(Calendar.DATE);
                mHour = cal.get(Calendar.HOUR_OF_DAY);
                mMinute = cal.get(Calendar.MINUTE);
                snooze = mYear + "-" + mMonth + "-" + mDay + " " + mHour
                        + "-" + (mMinute + minute);
                SimpleDateFormat df = new SimpleDateFormat(
                        "yyyy-MM-dd hh-mm");
                Date dt = null;
                try {
                    dt = df.parse(snooze);
                } catch (ParseException e) {
                    e.printStackTrace();
                } catch (java.text.ParseException e) {
                    e.printStackTrace();
                }
                when = dt.getTime();

                AlarmManager mgr = (AlarmManager) getApplicationContext()
                        .getSystemService(Context.ALARM_SERVICE);
                Intent notificationIntent = new Intent(
                        getApplicationContext(), ReminderAlarm.class);
                notificationIntent.putExtra("Name", name);

                notificationIntent.putExtra("Description", desc);

                notificationIntent.putExtra("NotifyCount",
                        notificationCount);
                PendingIntent pi = PendingIntent.getBroadcast(
                        getApplicationContext(), notificationCount,
                        notificationIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                mgr.set(AlarmManager.RTC_WAKEUP, when, pi);
                Toast.makeText(getApplicationContext(),
                        name +" Reminder Snoozed for " + minute + " Minutes " + notificationCount,
                        Toast.LENGTH_LONG).show();
            }

Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
Raja45
  • 11
  • 2

1 Answers1

0

I found the answer for this problem.. I set the ReminderPopup Activity as a transparent activity using "android:theme="@style/Theme_Dialog_Translucent">". When i set the snoozing only the AlertDialog will be disappaer not the ReminderPopup(Transparent) Activity. So when i set the next snoozing it will update the existing one. Add finish() at the end of AlertDialog for both Positive and Negative button..

Raja45
  • 11
  • 2