lundi 9 novembre 2015

How to set notification in android before specified date

I'm making an application where user can make some reminders for friend's birthdays, but i'm stuck with storing date type in database and also with setting some notification as i said in title before specified date.

How could i do that?

This is my code where i'm showing only on text button the date user chose, but i want to store that date and set notification 2-3 weeks before that specified date.

This is my code:

@SuppressLint("ValidFragment")
public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;

        // set selected date in button
        addBirthdayDate.setText(new StringBuilder().append(mMonth + 1)
        .append("-").append(mDay).append("-").append(mYear)
        .append("-"));
    }
}

And code in database(I have tried something in database, but i'm not really sure what i'm doing there) so i would appreciate any help here.:

public class DBHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 5;
private static final String DATABASE_NAME = "_database";

private static final String BIRTHDAY_TABLE_NAME = "birthday_table";
public static final String BIRTHDAY_ID = "birthday_id";
public static final String BIRTHDAY_NAME = "birthday_name";
public static final String BIRTHDAY_LAST_NAME = "birthday_last_name";
public static final String BIRTHDAY_GENDER = "birthday_gender";
public static final String BIRTHDAY_AGE = "birthday_age";
public static final String BIRTHDAY_DATE = "birthday_date";

private static final String CREATE_TABLE = "CREATE TABLE " + BIRTHDAY_TABLE_NAME + " ( "
        + BIRTHDAY_ID + " INTEGER PRIMARY KEY,"
        + BIRTHDAY_NAME + " TEXT,"
        + BIRTHDAY_LAST_NAME + " TEXT,"
        + BIRTHDAY_GENDER + " TEXT,"
        + BIRTHDAY_DATE + " DATETIME,"
        + BIRTHDAY_AGE + " INTEGER );";

SQLiteDatabase database;

public DBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + BIRTHDAY_TABLE_NAME);
    onCreate(db);
}

public String getDateTime() {
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    Date date = new Date();
    return dateFormat.format(date);
}

public void setBirthdayData(String birthdayName, String birthdayLastName, String birthdayGender
,int age) {
    database = getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(BIRTHDAY_NAME, birthdayName);
    cv.put(BIRTHDAY_LAST_NAME, birthdayLastName);
    cv.put(BIRTHDAY_GENDER, birthdayGender);
    cv.put(BIRTHDAY_AGE, age);
    cv.put(BIRTHDAY_DATE, getDateTime());
    database.insert(BIRTHDAY_TABLE_NAME, null, cv);
}

Aucun commentaire:

Enregistrer un commentaire