javaandroidcalendarlocaldatelocaltime

Is there a way to set alarm in android with local date and local time?


I am building a Calendar app to save events, show them and edit them with DatabaseHelper. I want to add alarm to these events but every I can find only setting alarm with Calendar import library. I want to set alarm in specific date and specific time.


Solution

  • I am building a Calendar app to save events show them and edit them with DatabaseHelper. I want to add alarm to these events but every I can find only setting alarm with Calendar import library. I want to set alarm in specific date and specific time.

    Thanks to Ole V.V.

    In this method i am creating a view for alert dialog, the function help me to convert date and time to start or cancel alarm:

      public View setAllFields(View view,String id, String title, String comment, String date, String time, String alarm)
        {
    
            TextView id_lv_tv = view.findViewById(R.id.id_lv_tv);
            TextView title_lv_tv =  view.findViewById(R.id.title_lv_tv);
            TextView comment_lv_tv = view.findViewById(R.id.comment_lv_tv);
            TextView date_lv_tv =  view.findViewById(R.id.date_lv_tv);
            TextView time_lv_tv =  view.findViewById(R.id.time_lv_tv);
            ImageView alarmState = view.findViewById(R.id.alarmState);
    
            id_lv_tv.setText(id);
            title_lv_tv.setText(title);
            comment_lv_tv.setText(comment);
            date_lv_tv.setText(date);
            time_lv_tv.setText(time);
            Calendar c = GregorianCalendar.from(LocalDate.parse(date).atTime(LocalTime.parse(time)).atZone(ZoneId.systemDefault()));
    
            if (alarm.equals("false"))
            {
                alarmState.setImageResource(R.drawable.alarm_off);
            }else if (alarm.equals("true"))
            {
                alarmState.setImageResource(R.drawable.alarm_on);
            }else
            {
                Toast.makeText(mContext, "Alarm set problem.", Toast.LENGTH_SHORT).show();
            }
            EventEdit ev = new EventEdit();
    
            alarmState.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (alarm.equals("false"))
                    {
                        startAlarm(c);
                        Toast.makeText(mContext, "Alarm is set to: " + c.toString().trim(), Toast.LENGTH_SHORT).show();
                        alarmState.setImageResource(R.drawable.alarm_on);
    
                    }else if (alarm.equals("true"))
                    {
                        cancelAlarm(c);
                        Toast.makeText(mContext, "Alarm cancelled from : " + c.toString().trim(), Toast.LENGTH_SHORT).show();
                        alarmState.setImageResource(R.drawable.alarm_off);
    
    
                    }
                }
            });
    
    
            return view;
        }