2

Hello Friends Need Help!

I'm working on Android, In my application there is a requirement to set multiple reminders at a time. Something like this

 for( int i = 0; i < n; i++)
 {
     // Code to set Reminder
 }

Currently I have following code, but that works fine only for one reminder at a time.

 StringTokenizer st=new StringTokenizer(strDateForReminder, "-");
             cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(st.nextToken()));
             cal.set(Calendar.MONTH, Integer.parseInt(st.nextToken())-1);
             cal.set(Calendar.YEAR, Integer.parseInt(st.nextToken()));

             String strTime= textView.getText().toString().trim();
            // Toast.makeText(getApplicationContext(), "strTime= "+strTime, Toast.LENGTH_LONG).show();

             String[] strTimeArray = strTime.split(getResources().getString(R.string.delimiter));
             String[] strFirstTime=strTimeArray[0].split(":");
             cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(strFirstTime[0]));
             cal.set(Calendar.MINUTE, Integer.parseInt(strFirstTime[1]));
             cal.set(Calendar.SECOND, 00);

             Intent intent = new Intent(Intent.ACTION_EDIT);
             intent.setType("vnd.android.cursor.item/event");
             intent.putExtra("beginTime", cal.getTimeInMillis());
             intent.putExtra("endTime", cal.getTimeInMillis()+90*60*1000);
             intent.putExtra("title", "Reminder");
             startActivity(intent);

Please Help. Thanks in Advance!

Dnyanesh M
  • 1,349
  • 4
  • 18
  • 46

2 Answers2

5

If I understand correctly, your approach using an activity only allows you to add one event at a time because the user has to interact with the device to confirm it. What you want is the new CalendarContract introduced in 4.0.

From Android Cookbook:

The ContentProvider-based method may be preferable if you do not want the user to have to interact with a calendaring application. In Froyo and Gingerbread and Honeycomb releases, you had to "know" the names to use for the various fields you wanted to interact with. We do not cover this method as it is officially unsupported, but you can find a good article on the web by our contributor Jim Blacker, at http://jimblackler.net/blog/?p=151.

Effective with Ice Cream Sandwich (Android 4, API level 14), the new CalendarContract class holds, in a variety of nested classes, all the constants needed to make a portable calendar application. This is shown inserting a Calendar Event directly into the user's first Calendar (using id 1); obviously there should be a drop-down listing the user's calendars in a real application.

public void addEvent(Context ctx, String title, Calendar start, Calendar end) {
    Log.d(TAG, "AddUsingContentProvider.addEvent()");
        
    TextView calendarList = 
        (TextView) ((Activity) ctx).findViewById(R.id.calendarList);
        
    ContentResolver contentResolver = ctx.getContentResolver();
        
    ContentValues calEvent = new ContentValues();
    calEvent.put(CalendarContract.Events.CALENDAR_ID, 1); // XXX pick)
    calEvent.put(CalendarContract.Events.TITLE, title);
    calEvent.put(CalendarContract.Events.DTSTART, start.getTimeInMillis());
    calEvent.put(CalendarContract.Events.DTEND, end.getTimeInMillis());
    calEvent.put(CalendarContract.Events.EVENT_TIMEZONE, "Canada/Eastern");
    Uri uri = contentResolver.insert(CalendarContract.Events.CONTENT_URI, calEvent);
        
    // The returned Uri contains the content-retriever URI for 
    // the newly-inserted event, including its id
    int id = Integer.parseInt(uri.getLastPathSegment());
    Toast.makeText(ctx, "Created Calendar Event " + id,
        Toast.LENGTH_SHORT).show();
}
Community
  • 1
  • 1
Rahat Ahmed
  • 2,191
  • 2
  • 30
  • 40
-4

I think what you're looking for is AlarmManager.

m0skit0
  • 25,268
  • 11
  • 79
  • 127