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!
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();
}
I think what you're looking for is AlarmManager.
Related
I'm currently using this code to add an event to a calendar in the user's device:
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra(CalendarContract.Events.TITLE, mission.company.name);
intent.putExtra(CalendarContract.Events.EVENT_LOCATION, mission.service.address);
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startCal.getTimeInMillis());
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endCal.getTimeInMillis());
intent.putExtra(CalendarContract.Events.ALL_DAY, false);// periodicity
intent.putExtra(CalendarContract.Events.DESCRIPTION,
function.name + "\n\n" + mission.service.address + "\n\n" + mission.service.phone
);
String title = "Choose the calendar you want to save the event to";
Intent chooser = Intent.createChooser(intent, title);
try {
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
} catch (ActivityNotFoundException e) {
Toast.makeText(this, getString(R.string.toast_no_application_found_for_action), Toast.LENGTH_SHORT).show();
}
My problem is that this will only add ONE event. I have cases where in the same time interval (i.e. 12:00PM - 13:00PM) needs to repeat for 2-3 days (Monday, Tuesday, Wednesday). How can I change the code (i want to use Intents) to accomplish this? If I just set the start of the event on Monday at 12:00PM and say it'll end on Wednesday on 13:00PM that's no good, as it'll set one long event.
Any ideas ?
You could add a new extra to your Intent to set the CalendarContract.Events.RRULE field to the value "FREQ=DAILY;COUNT=x"(where x is the number of days for the recurring event).
I am trying to add an event on a calendar that i created programmatically like this
var uri = CalendarContract.Calendars.ContentUri; ContentValues val = new ContentValues();
val.Put(CalendarContract.Calendars.InterfaceConsts.CalendarAccessLevel, CalendarAccess.AccessOwner.ToString());
val.Put(CalendarContract.Calendars.Name, "Mary");
val.Put(CalendarContract.Calendars.InterfaceConsts.Visible, true);
val.Put(CalendarContract.Calendars.InterfaceConsts.SyncEvents, true);
val.Put(CalendarContract.Calendars.InterfaceConsts.CalendarColor, "0xff00ffff");
val.Put(CalendarContract.Calendars.InterfaceConsts.CalendarDisplayName, "Mary");
uri = uri.BuildUpon()
.AppendQueryParameter(CalendarContract.CallerIsSyncadapter, "true")
.AppendQueryParameter(CalendarContract.Calendars.InterfaceConsts.AccountName, AccountName)
.AppendQueryParameter(CalendarContract.Calendars.InterfaceConsts.AccountType, CalendarContract.AccountTypeLocal)
.Build();
var calresult = ContentResolver.Insert(uri, val);
calID = int.Parse(calresult.LastPathSegment);
AddCalendarEvent( calID);
it gets added successfully and I am able to see this calendar
I am adding event like this,
ContentValues eventValues = new ContentValues();
eventValues.Put(CalendarContract.Events.InterfaceConsts.CalendarId, calID);
eventValues.Put(CalendarContract.Events.InterfaceConsts.Title, "Test Event from M4A");
eventValues.Put(CalendarContract.Events.InterfaceConsts.Description, "This is an event created from Mono for Android");
eventValues.Put(CalendarContract.Events.InterfaceConsts.Dtstart, GetDateTimeMS(2013, 9, 15, 10, 0));
eventValues.Put(CalendarContract.Events.InterfaceConsts.Dtend, GetDateTimeMS(2013, 9, 15, 11, 0));
// GitHub issue #9 : Event start and end times need timezone support.
// https://github.com/xamarin/monodroid-samples/issues/9
eventValues.Put(CalendarContract.Events.InterfaceConsts.EventTimezone, "UTC");
eventValues.Put(CalendarContract.Events.InterfaceConsts.EventEndTimezone, "UTC");
var uri = ContentResolver.Insert(CalendarContract.Events.ContentUri, eventValues);
Console.WriteLine("Uri for new event: {0}", uri);
My problem is that when I am trying to open this event to see the description, it gives me an error that "Unfortunately the calendar has stopped". I am able to open the event when it is added to the default calendar. What wrong am I doing in the creating the calendar??
I followed the code from this blog, http://www.derekbekoe.co.uk/blog/16-using-the-android-4-0-calendar-api#part4 and still it is giving me the same error. I am using Android 4.2.2
When creating the calendar, you should be specifying the OWNER_ACCOUNT. This link from the Android docs shows you the fields that must be included when inserting a new calendar. (http://developer.android.com/reference/android/provider/CalendarContract.Calendars.html)
This is similarly the case for adding a new event to your calendar. (http://developer.android.com/reference/android/provider/CalendarContract.Events.html)
Also, it is not clear from your code but currently it looks like you are using the default uri for events, CalendarContract.Events.ContentUri. You may have to build the event uri also, just like you have done for creating the calendar, and then use this new uri when you insert the event with ContentResolver.Insert.
The problem was the calendar was not liking the color code set. I used Android.Graphics.Color.Red instead of the hash code for color. It works well with that
The code i'm working on lists the events from the calendar, i need to limit the range of dates, and in the examples i see this code:
// Construct the query with the desired date range.
Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, startMillis);
ContentUris.appendId(builder, endMillis);
I don't understand why appendId is used in this way. startMillis and endMillis are not ids, i would expect that the parameter name had to be provided eg "startdate" , It's not clear to me why this works, and what other parameters could be specified this way. Are there more parameters supported by appenedId? How can i know?
What appendId actually does is add a /# to your uri where # is a number. In your example (assuming startMillis = 1000 and endMillis = 3000 and the uri content://com.google.calendar/) this would mean your uri could end up like this:
content://com.google.calendar/1000/3000
This is something that a uri parser can pickup:
URIMatcher.addURI(AUTHORITY, calendar + "/#/#", DATE_RANGE);
Long story short: appendId is just a convenient and type-safe way to add an integer to your uri path.
I too have been trying to understand more about ContentUris as I had a section of code that wasn't working within the CalendarContract Instances table. This is strange because I didn't need to pass these in for the Calendars or Events table queries that I have developed.
So I added the appendId statements and passed in the current time in UTC for both values and the code now works. The actual query in my code is using the current time to looking for current events - please see the code below. If I take the appendID statements out an exception is raised - I think it was something like Content Provider URL not found.
String instanceQuery = "EVENT_ID = " + event_id +
" AND begin <= " + now +
" AND end >= " + now;
Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(eventsUriBuilder, now);
ContentUris.appendId(eventsUriBuilder, now);
Uri eventsUri = eventsUriBuilder.build();
Cursor instanceCursor = null;
instanceCursor = ctx.getContentResolver().query(eventsUri,
new String[] { CalendarContract.Instances.EVENT_ID,
CalendarContract.Instances.BEGIN,
CalendarContract.Instances.END},
instanceQuery,
null,
null);
My code is working but I would like to know what impact the appendID statements actually have, e.g. do the values add any constraints. It looks like my actual query is overriding any implied range that is passed in and I really don't understand why they are required.
Hopefully a brief explanation from someone who understands this more would benefit the developer community....
What I want: I want to add calendar events in Android 2.2.
What I Have: I have added an event using the below code
Uri calendars = Uri.parse("content://com.android.calendar/events");
Cursor managedCursor = managedQuery(calendars, null, null, null, null);
startManagingCursor(managedCursor);
managedCursor.moveToFirst();
String ID = null;
do
{
ID = managedCursor.getString(managedCursor.getColumnIndexOrThrow("_id"));
}
while (managedCursor.moveToNext());
managedCursor.close();
int NewID = Integer.parseInt(ID) + 1;
ContentValues event = new ContentValues();
event.put("calendar_id", NewID); // --- Some confusion Here with the ID,
// --- not sure how to use it here
event.put("title", "New Event Title");
event.put("description", "Event Desc");
event.put("eventLocation", "Somewhere");
long startTime = System.currentTimeMillis() + 1000 * 60 * 60;
long endTime = System.currentTimeMillis() + 1000 * 60 * 60 * 2;
event.put("dtstart", startTime);
event.put("dtend", endTime);
event.put("allDay", 0); // 0 for false, 1 for true
event.put("eventStatus", 1);
event.put("visibility", 0);
event.put("transparency", 0);
event.put("hasAlarm", 0); // 0 for false, 1 for true
Uri eventsUri = Uri.parse("content://com.android.calendar/events");
Uri insertedUri = getContentResolver().insert(eventsUri, event);
What is the problem:
So far I have been successful in adding a single event on the specified date time, and apparently NewID's role is suspicious to me. When I try to add some other event, I get the returned Uri insertedUri and it shows me the newly added ID at the end of the URI. But I cant see any such event on the device. May be there is some problem in my understanding of the Calendar and events, or differences in both and their ID's. Kindly guide me what I am missing or doing wrong.
Regards,
Khawar
Most of the code is fine, you just need to know a little bit of concepts regarding calendar. Actually there is more than one calendar types in Android.
To Traverse all the calendars, Use following Uri for 2.2:
Uri calendars = Uri.parse("content://com.android.calendar"+ "/calendars");
And get the values of 'id' and 'name', you will get the idea.
NewID's role is suspicious to me
Your insertion code is fine, you just need to give the id of that calendar in which you want to insert any event.
I still believe that even myself needs to learn alot so if you have anything to tell or correct, you are most welcome. Following links helped me:
Working with Android Calendars
Accessing Calendars events without using gdata api
Hello m
I am new to android..
I have samsung galaxy 3 and android 2.1 installed in it..
I want to build an widget as follows..
1.I want to access calendar of phone.. (googled a lot found diffrent answers but none wokrin..) (jimblackler.net/blog/?p=151 not working)..
since no public api is available it will be ok even if am able to use google calendar(the one from web).. letme know whichever is easier to implement. I just want to read events..
2.How do I trigger events on a specific time.. say when it is 3:00hrs it should call a function..
Is it possible ? if so how
3.I want to send two strings to a server and recieve andother text back as result. which would be the easiest way to code ?? have an c# web service..? or just use a php script on server accepting few parameters?
thanks a lot. :)
First Question: Calendar access: This is based on the content://calendar/calendars URI.
First, make sure your application defines the permission android.permission.READ_CALENDAR.
Get the calendars IDs and display names:
ContentResolver myContentResolver = getContentResolver();
final Cursor cursor = myContentResolver.query(Uri.parse("content://calendar/calendars"),
(new String[] { "_id", "displayName", "selected" }), null, null, null);
while (cursor.moveToNext()) {
final String _id = cursor.getString(0);
final String displayName = cursor.getString(1);
final Boolean selected = !cursor.getString(2).equals("0");
}
Work on a specific calendar:
Uri.Builder myUriBuilder = Uri.parse("content://calendar/instances/when").buildUpon();
long currentTime = new Date().getTime();
ContentUris.appendId(myUriBuilder , currentTime - DateUtils.WEEK_IN_MILLIS);
ContentUris.appendId(myUriBuilder, currentTime + DateUtils.WEEK_IN_MILLIS);
Cursor eventCursor = myContentResolver.query(myUriBuilder.build(),
new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + id,
null, "startDay ASC, startMinute ASC");
while (eventCursor.moveToNext()) {
// fields:
// Event title - String - 0
String eventTitle = eventCursor.getString(0);
// Event start time - long - 1
Date startTime = new Date(eventCursor.getLong(1));
// Event end time - login - 2
Date endTime = new Date(eventCursor.getLong(2));
// Is it an All Day Event - String - 3 (0 is false, 1 is true)
Boolean allDay = !eventCursor.getString(3).equals("0");
}
Second Question: Scheduling Events:
Basically, you should use the AlarmManager class, see the android SDK for details.
Example to set the event 3 hours from now:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 3);
Intent intent = new Intent(this, MyAlarmHandler.class);
intent.putExtra("alarm_message", "Wake up!");
int requestCode = 3824723; // this is not currently used by Android.
PendingIntent pIntent = PendingIntent.getBroadcast(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pIntent);
Third Question: Yes, I suggest the PHP approach