mardi 23 juin 2015

How to cancel alarm in android after deleting a item in Sqlite database

I have a visit reminder activity where I am getting info like name, date, time and reminds before time. After button click I am saving this info in local sqlite database. In the save button click, I am getting every info and then set alarm for generating notification. I also have an option to see all reminders where I can delete or update my reminders. But when I delete my reminder, I am still getting notification. For saving data in sqlite and retrieving this data for setting alarm, I have written this code

public void saveButtonClick(View view) {

    long timeInMilliseconds = calendar1.getTimeInMillis();

    // Visit Reminder database for inserting patient visit data
    VisitReminderDatabase database = new VisitReminderDatabase(this);
    database.addVisitReminderItem(new VisitReminderModel(doctorName.getText().toString(), setDateValue, setTimeValue,
            timeInMilliseconds, spinnerValue));

    getDataFromDatabase();

    Toast.makeText(getApplicationContext(), "You have successfully added your reminder", Toast.LENGTH_SHORT).show();
}

public void getDataFromDatabase(){

    VisitReminderDatabase database = new VisitReminderDatabase(getApplicationContext());
    List<VisitReminderModel> list = database.getAllVisitReminders();

    for (VisitReminderModel model : list){
        Log.v("Id", String.valueOf(model.getId()));
        Log.v("Doctor Name", model.getDoctorName());
        Log.v("Set Date", model.getDate());
        Log.v("Set Time", model.getTime());
        Log.v("Time In milliseconds", String.valueOf(model.getTimeInMilliseconds()));
        Log.v("Reminds Before", model.getRemindsBefore());

        createPendingIntent(model.getDoctorName(), model.getId(), model.getTime(), model.getDate());

        createReminders(model.getTimeInMilliseconds(), model.getRemindsBefore());
    }
}

private void createPendingIntent(String name, int id, String time, String date){

    Intent intent = new Intent(getApplicationContext(), VisitReminderNotificationMessage.class);
    intent.putExtra("Id", id);
    intent.putExtra("Doctor Name", name);
    intent.putExtra("Date", date);
    intent.putExtra("Time", time);

    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, 0);
}

private void createReminders(long timeInMilliseconds, String remindsBefore){

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

//alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMilliseconds, AlarmManager.INTERVAL_DAY, pendingIntent);

long newTimeInMilliseconds;
if (remindsBefore.equals("1min")) {
    newTimeInMilliseconds = timeInMilliseconds - 1 * 60 *1000;

    alarmManager.set(AlarmManager.RTC_WAKEUP, newTimeInMilliseconds, pendingIntent);
} else if (remindsBefore.equals("2min")) {

    newTimeInMilliseconds = timeInMilliseconds - 2* 60 * 1000;

    alarmManager.set(AlarmManager.RTC_WAKEUP, newTimeInMilliseconds, pendingIntent);
} else if (remindsBefore.equals("3min")) {
    //timeInMilliseconds = calendar1.getTimeInMillis();

    //long oneDay = AlarmManager.INTERVAL_DAY; //Convert 24 Hrs to milliseconds
    //long reminderTimes = timeInMilliseconds - oneDay; // Difference between set alarm date and one day before

    newTimeInMilliseconds = timeInMilliseconds - 3 * 60 * 1000;

    alarmManager.set(AlarmManager.RTC_WAKEUP, newTimeInMilliseconds, pendingIntent);
}

}

For displaying all reminders I have written this code

private class CustomAdapter extends BaseAdapter implements ListAdapter{

private Context context;
private List<VisitReminderModel> data;

public CustomAdapter(Context context, List<VisitReminderModel> data){
    this.context = context;
    this.data = data;
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView == null){

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_visit_reminder_list, parent, false);

        holder = new ViewHolder();
        holder.mDoctor = (TextView) convertView.findViewById(R.id.doctorNameTextView);
        holder.mDate = (TextView) convertView.findViewById(R.id.dateTextView);
        holder.mTime = (TextView) convertView.findViewById(R.id.timeTextView);
        holder.mRemindsBefore = (TextView) convertView.findViewById(R.id.remindsBeforeTextView);
        holder.mEdit = (Button) convertView.findViewById(R.id.editButton);
        holder.mDelete = (Button) convertView.findViewById(R.id.deleteButton);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final VisitReminderModel model = data.get(position);

    holder.mDoctor.setText("Doctor Name " + model.getDoctorName());
    holder.mDate.setText("Date " + model.getDate());
    holder.mTime.setText("Time " + model.getTime());
    holder.mRemindsBefore.setText("Reminds Before " + model.getRemindsBefore());

    holder.mDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage("Do you really want to delete");
            builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    Log.v("Time In milliseconds", String.valueOf(data.get(position).getTimeInMilliseconds()));
                    Log.v("Reminds Before Time", data.get(position).getRemindsBefore());

                    database.deleteVisitReminderItem(data.get(position));
                }
            });

            builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            Dialog dialog = builder.create();
            dialog.show();
        }
    });

    return convertView;
}

private class ViewHolder{
    private TextView mDoctor;
    private TextView mDate;
    private TextView mTime;
    private TextView mRemindsBefore;

    private Button mEdit;
    private Button mDelete;
}

}

I want to cancel alarm that I have set before when I delete that item from database. Please somebody help me..

Aucun commentaire:

Enregistrer un commentaire