Explanation: I had created a calendar in my application.Which provides the functionality like add and delete event on the particular day of the month. This event is saved in my SQLite database and display from the db.Whatever, i explained almost things i did.
What i want?
Suppose, i added an event of birthday of my friend on 14th dec in 2016.So, this event will fired at the time when my friend birthday will arrive.
I want to fired that event on particular day like alarm.
Here is my calendar code
public class CalendarFragment extends Fragment{
View rootView;
String str_date="";
String month="";
String year="";
ListView event_list;
String parts[];
String monthYear="";
String monthAyear="";
private SimpleDateFormat dateFormatForMonth = new SimpleDateFormat("MMM - yyyy", Locale.getDefault());
EventAdapter adapter=null;
DatabaseHandler db=null;
List<Event> list_data;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView=inflater.inflate(R.layout.fragment_calendar, container, false);
event_list=(ListView)rootView.findViewById(R.id.event_listview);
final ImageButton showPreviousMonthBut = (ImageButton) rootView.findViewById(R.id.prev_button);
final ImageButton showNextMonthBut = (ImageButton) rootView.findViewById(R.id.next_button);
db=new DatabaseHandler(getContext());
final TextView year_name=(TextView)rootView.findViewById(R.id.year_name);
final CompactCalendarView compactCalendarView = (CompactCalendarView) rootView.findViewById(R.id.compactcalendar_view);
compactCalendarView.drawSmallIndicatorForEvents(true);
compactCalendarView.setCurrentDayBackgroundColor(getResources().getColor(R.color.black));
compactCalendarView.setCurrentSelectedDayBackgroundColor(getResources().getColor(R.color.dark_red));
compactCalendarView.invalidate();
year_name.setText(dateFormatForMonth.format(compactCalendarView.getFirstDayOfCurrentMonth()));
monthYear=dateFormatForMonth.format(compactCalendarView.getFirstDayOfCurrentMonth());
parts=monthYear.split("-");
month=parts[0].replace(" ","");
year=parts[1].replace(" ","");
monthAyear=month+"-"+year;
list_data=db.getAllEvents();
showData(monthAyear);
compactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener() {
@Override
public void onDayClick(Date dateClicked) {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd-MMM-yyyy");
str_date=simpleDateFormat.format(dateClicked);
dialogShow();
}
@Override
public void onMonthScroll(Date firstDayOfNewMonth) {
monthYear=dateFormatForMonth.format(firstDayOfNewMonth);
parts=monthYear.split("-");
month=parts[0].replace(" ","");
year=parts[1].replace(" ","");
monthAyear=month+"-"+year;
showData(monthAyear);
year_name.setText(dateFormatForMonth.format(firstDayOfNewMonth));
}
});
showPreviousMonthBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compactCalendarView.showPreviousMonth();
}
});
showNextMonthBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compactCalendarView.showNextMonth();
}
});
//
return rootView;
}
public void dialogShow(){
final Dialog dialog=new Dialog(getContext());
dialog.setContentView(R.layout.dialog_layout);
dialog.setTitle("Add Event");
final TextView event_name=(TextView)dialog.findViewById(R.id.event_name);
final TextView event_desc=(TextView)dialog.findViewById(R.id.event_description);
final TextView event_date=(TextView)dialog.findViewById(R.id.event_date);
event_date.setText(str_date);
Button btn_save=(Button)dialog.findViewById(R.id.btn_save);
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.addEvent(new Event(event_name.getText().toString(),event_desc.getText().toString(),event_date.getText().toString(),monthAyear));
dialog.hide();
Toast.makeText(getContext(), "Event added successfully....", Toast.LENGTH_SHORT).show();
showData(monthAyear);
}
});
dialog.show();
}
public void showData(String monthWyear){
List<Event> list_data=db.getEvent(monthWyear);
adapter=new EventAdapter(getActivity(),list_data);
event_list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.findItem(R.id.spinner).setVisible(false);
super.onCreateOptionsMenu(menu, inflater);
}
}
Please, help me What will i do?
You need to use the AlarmManager Class to notify the user through Push Notifications:
So, to get this working, you need to pass the time in milliseconds which represents your target date e.g : 24 Dec 2016 -> Convert to milliseconds and pass it to the AlaramManager
}
}
You can alarm manager class and put your broadcast receiver to handle that event and for showing event in dialog you can make dialog as an activity which shows your event detail from database
For putting alarm
in broad cast receiver