0

I am new in android try to make a reminder app which stores date taken by user,This is a reminder app, i want to push notification if date stored in database is equals to current date of system, but notification doesn't work properly and also want to compare date in mm-dd format

 public class MainActivity extends Activity {

        private static final int CONTACT_PICKER_RESULT = 0;
        private static final String DEBUG_TAG = null;
        EditText e1, e2;
        Button b1;
        entry en;
        String dob[];
        private String form[] = { "name", "contact_no", "dob" };
        private Button button;
        private int year;
        private int month;
        private int day;
        static final int DATE_DIALOG_ID = 100;
        private TextView Output;
        private Button changeDate;
        private SQLiteDatabase database;

        static final int DATE_PICKER_ID = 1111;
        Cursor c;

        NotificationManager NM;

        Date d1 = new Date();

        SimpleDateFormat format;

        // public static final String[] ALL_KEYS = new String[] {name , contact_no ,
        // dob};

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            e1 = (EditText) findViewById(R.id.editText1);
            e2 = (EditText) findViewById(R.id.editText2);
            b1 = (Button) findViewById(R.id.button1);
            // setCurrentDate();
            // addButtonListener();

            Output = (TextView) findViewById(R.id.Output);
            changeDate = (Button) findViewById(R.id.changeDate);

            // Get current date by calender

            final Calendar c1 = Calendar.getInstance();
            year = c1.get(Calendar.YEAR);
            month = c1.get(Calendar.MONTH);
            day = c1.get(Calendar.DAY_OF_MONTH);

            // Show current date

            Output.setText(new StringBuilder()
                    // Month is 0 based, just add 1
                    .append(month + 1).append("-").append(day).append("-")
                    .append(year).append(" "));

            // Button listener to show date picker dialog

            changeDate.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    // On button click show datepicker dialog
                    showDialog(DATE_PICKER_ID);

                }

            });

            b1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    SubmitData();

                }
            });

        }

        private void SubmitData() {
            en = new entry(this);
            try {
                String n = e1.getText().toString();
                String c = e2.getText().toString();

                String d = Output.getText().toString();
                // System.out.println(d);
                final Calendar c1 = Calendar.getInstance();
                month = c1.get(Calendar.MONTH);
                day = c1.get(Calendar.DAY_OF_MONTH);
                TextView p = null;
                // String p;
                p.setText(new StringBuilder()
                // Month is 0 based, just add 1
                        .append(month + 1).append("-").append(day).append(" "));
                String date1 = p.getText().toString();

                // Show current date
                addentry(n, c, d);

                if (d == date1) {
                    notify();
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                en.close();
            }

        }

        private void showReg(Cursor cursor) {
            // TODO Auto-generated method stub
            StringBuilder builder = new StringBuilder("saved data:\n");

            while (cursor.moveToNext()) {
                builder.append(cursor.getString(0) + ":");
                builder.append(cursor.getString(1) + ":");
                builder.append(cursor.getString(2) + ":");

                builder.append("\n");
            }
            e2.setText(builder.toString());

        }

        private void addentry(String n, String c, String d) {
            // TODO Auto-generated method stub

            SQLiteDatabase database = en.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put("fname", n);
            contentValues.put("contno", c);
            contentValues.put("dob", d);

            database.insertOrThrow("datbod", null, contentValues);
            Toast.makeText(getApplicationContext(), "'Birth day data saved", 4000)
                    .show();
        }

        public Cursor getReg() {
            // TODO Auto-generated method stub
            SQLiteDatabase database = en.getReadableDatabase();
            Cursor cursor = database.query("datbod", form, null, null, null, null,
                    null);
            startManagingCursor(cursor);
            return cursor;
        }

        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DATE_PICKER_ID:

                // open datepicker dialog.
                // set date picker for current date
                // add pickerListener listner to date picker
                return new DatePickerDialog(this, pickerListener, year, month, day);
            }
            return null;
        }

        private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

            // when dialog box is closed, below method will be called.
            @SuppressWarnings("deprecation")
            @Override
            public void onDateSet(DatePicker view, int selectedYear,
                    int selectedMonth, int selectedDay) {

                year = selectedYear;
                month = selectedMonth;
                day = selectedDay;

                // Show selected date
                Output.setText(new StringBuilder().append(month + 1).append("-")
                        .append(day).append(" "));

            }

        };

        public void doLaunchContactPicker(View view) {
            Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
                    Contacts.CONTENT_URI);
            startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
        }

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                switch (requestCode) {
                case CONTACT_PICKER_RESULT:
                    Cursor cursor = null;
                    String name = "";
                    String number = "";

                    try {
                        Uri result = data.getData();
                        Log.v(DEBUG_TAG,
                                "Got a contact result: " + result.toString());

                        // get the contact id from the Uri
                        String id = result.getLastPathSegment();

                        // query for name
                        cursor = getContentResolver().query(Phone.CONTENT_URI,
                                null, Phone.CONTACT_ID + "=?", new String[] { id },
                                null);

                        if (cursor != null && cursor.moveToFirst()) {
                            int phoneIdx = cursor.getColumnIndex(Phone.DATA);
                            int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
                            name = cursor.getString(nameIdx);

                            int phoneIdx1 = cursor.getColumnIndex(Phone.DATA);
                            int numberIdx = cursor.getColumnIndex(Phone.NUMBER);
                            number = cursor.getString(numberIdx);
                        }

                        cursor = getContentResolver().query(Phone.CONTENT_URI,
                                null, Phone.CONTACT_ID + "=?", new String[] { id },
                                null);
                    } catch (Exception e) {
                        Log.e(DEBUG_TAG, "Failed to get name", e);
                    } finally {
                        if (cursor != null) {
                            cursor.close();
                        }
                        e1 = (EditText) findViewById(R.id.editText1);
                        e1.setText(name);
                        if (name.length() == 0) {
                            Toast.makeText(this, "Name not found for contact.",
                                    Toast.LENGTH_LONG).show();
                        }

                        e2 = (EditText) findViewById(R.id.editText2);
                        e2.setText(number);
                        if (number.length() == 0) {
                            Toast.makeText(this, "Name not found for contact.",
                                    Toast.LENGTH_LONG).show();
                        }
                    }

                    break;
                }

            } else {
                Log.w(DEBUG_TAG, "Warning: activity result not ok");
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @SuppressWarnings("deprecation")
        public void notify(View vobj) {
            String title = e1.getText().toString();
            String subject = e2.getText().toString();
            String body = Output.getText().toString();
            NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notify = new Notification(
                    android.R.drawable.stat_notify_more, title,
                    System.currentTimeMillis());
            PendingIntent pending = PendingIntent.getActivity(
                    getApplicationContext(), 0, new Intent(), 0);
            notify.setLatestEventInfo(getApplicationContext(), subject, body,
                    pending);
            NM.notify(0, notify);
        }

    }
  • Dates shouldn't be checked for equality, but rather you should check whether a Date is later than another. Dates are `Comparable`, enabling making this easily – Antoine Marques Aug 08 '14 at 07:33

1 Answers1

0

you have to use "equals" to compare strings in java, have a look at How do I compare strings in Java?

Community
  • 1
  • 1
jmhostalet
  • 4,399
  • 4
  • 38
  • 47