I'm trying to insert events in to a calendar i created. I'm using the following code to do so... it's taken from google's documentation on CalendarProvider.
The problem is that no matter what values i put in DTSTART or DTEND the event is always created as a single day event and the date is the current date.
I've also tried to use Intent(Intent.ACTION_EDIT) with .putExtra for the dates... but sadly this doesn't help as well... neither the dates are set nor it gives me to modify them in the edit intent.
Any Idea what might be causing this behavior?
ContentValues event = new ContentValues();
event.put(Events.CALENDAR_ID, e.getCalendarId());
event.put(Events.TITLE, e.getTitle());
event.put(Events.DESCRIPTION, e.getDescription());
event.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getDisplayName());
event.put(Events.EVENT_END_TIMEZONE, TimeZone.getDefault().getDisplayName());
event.put(Events.GUESTS_CAN_MODIFY, 1);
event.put(Events.IS_ORGANIZER, 1);
event.put(Events.DTSTART, e.getDateStart());
event.put(Events.DTEND, e.getDateEnd());
event.put(Events.ALL_DAY, 1);
event.putNull(Events.DURATION);
event.putNull(Events.RRULE);
event.putNull(Events.RDATE);
Uri eventUri = context.getContentResolver().insert(Events.CONTENT_URI, event);
int eventID = Integer.parseInt(eventUri.getLastPathSegment());
Related
i use this code to insert the value in calander,using the for loop i am inserting multiple values in diffrent date,but i got stuck in this place.
JSONObject jsonObject = jsonArray.getJSONObject(L);
year[L] = jsonObject.getString("year");
month[L] = jsonObject.getString("month");
day[L] = jsonObject.getString("day");
StartTime[L] = jsonObject.getString("StartTime");
Endtime[L] = jsonObject.getString("Endtime");
Hours[L] = jsonObject.getString("Hours");
final ContentValues event = new ContentValues();
event.put(CalendarContract.Events.CALENDAR_ID, 1);
event.put(CalendarContract.Events.TITLE, "WORKING SCHEDULE OF THE WEEK");
event.put(CalendarContract.Events.DESCRIPTION, "selva");
event.put(CalendarContract.Events.EVENT_LOCATION, "chennai");
after i added the events in calander i need to get the event id of that event created.
which means, if the for loop is executed for 5 times to insert the values in diffrent dates of calander,
1)when the for loop is executed for the first time ,it should insert the event and it should return the id of that particular event and it should store in array.
i used this code to retrive the id of the all the event added in the calander.
String event_ID = cur1.getString(cur1.getColumnIndex(CalendarContract.Events._ID));
but,i need to get only event id of events created by me,every time when the for loop is excuted for the particulr event added.
can anyone help to retrive the event id ?
Uri ref=getApplicationContext().getContentResolver().insert(baseUri, event)
When changing the timezone of an event with both EVENT_TIMEZONE and EVENT_END_TIMEZONE set, for all calendar apps I tried (aCalendar 0.99.8, Google's default calendar 4.4.2), EVENT_END_TIMEZONE is not updated to the new timezone, although the end time is displayed according to the new timezone.
Why isn't the end timezone set to the same value as the start/regular timezone? Am I missing some other timezone-related fields that have to be set in order for EVENT_END_TIMEZONE to be used / updated?
As I was asked to provide some code, here is one possible way to define an event showing the described behavior (note that, depending on the context of your code, you may need to use a different URI and/or add additional fields):
final ContentResolver cr = /* ... */;
final long calendarId = /* ... */;
final ContentValues values = new ContentValues();
values.put(CalendarContract.Events.CALENDAR_ID, calendarId);
values.put(CalendarContract.Events.TITLE, "Test");
values.put(CalendarContract.Events.DTSTART, 1428307200000L);
values.put(CalendarContract.Events.EVENT_TIMEZONE, "Europe/Berlin");
values.put(CalendarContract.Events.DTEND, 1428314400000L);
values.put(CalendarContract.Events.EVENT_END_TIMEZONE,"Europe/Berlin");
final Uri result = cr.insert(CalendarContract.Events.CONTENT_URI, values);
Once the event is changed to another timezone using any of the two apps listed above, its EVENT_END_TIMEZONE is preserved ("Europe/Berlin"), while the event's end is displayed relative to the new timezone (something different from "Europe/Berlin").
I am new to android. I am getting a problem while restoring the call log, which I stored in a database.
I am storing the call log with the following code:
Cursor managedCursor = cr.query(CallLog.Calls.CONTENT_URI, null,
CallLog.Calls.NUMBER + "=?",
new String[] {(ActiveUserContacts.get(i).getnumber()) },
null);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
int NEW = managedCursor.getColumnIndex(CallLog.Calls.NEW);
while (managedCursor.moveToNext()) {
CallLogsModel Log = new CallLogsModel(Integer.toString(i),
managedCursor.getString(type),
managedCursor.getString(date),
managedCursor.getString(duration),
managedCursor.getString(number),
managedCursor.getString(name),
managedCursor.getString(NEW));
StoreData.addCallLog(UserNAME, Log);
}
managedCursor.close();
And I restore it with the code:
ContentValues values = new ContentValues();
values.put(CallLog.Calls.TYPE, PrevContents.get(i).getType());
values.put(CallLog.Calls.DATE, PrevContents.get(i).getDate());
values.put(CallLog.Calls.DURATION, PrevContents.get(i).getDuration());
values.put(CallLog.Calls.NUMBER, PrevContents.get(i).getNumber());
values.put(CallLog.Calls.CACHED_NAME, PrevContents.get(i).getName());
values.put(CallLog.Calls.NEW, PrevContents.get(i).getNew());
getActivity().getContentResolver().insert(CallLog.Calls.CONTENT_URI, values);
However, everything but the time of call got restored. Did I make a mistake?
Your PrevContents.get(i).getDate() may not be the right type or in the right format for the call log. Some of the examples I see of records inserted into the call log (e.g., android adding number to Call logs) use System.currentTimeMillis() as the date, which is actually of type long. You probably want to use managedCursor.getLong(date) in the code you use to retrieve the date, and store it as a long.
Another note: it looks as if you're getting 1 record when you query the call log, put that in your cursor, save that 1 record, then do the same thing for the next record, and get all the records by looping through all of the call log records manually (I assume that's what that index 'i' is for). You don't need to do that--the query can get all records for you, then you can use managedCursor.moveToNext() to do the looping. Take a look at http://android2011dev.blogspot.com/2011/08/get-android-phone-call-historylog.html for an example of how to do this. When you do the restoration you may need a loop (though there may be an easier way to do that, too).
I'm just getting up to speed on Android, and I want to send calendar event to others from mobile. Same as whatsapp sending contacts. For sending Contact in chat (XMPP) I've used vCard. So for sending calendar event in chat what should I use?
I have searched a lot. But can't find something fruitful.
Please suggest the library or code snippet for sending calendar event in XMPP.
Thanks in advance.
You may refer to iCal Import Export allows you to import iCalender files to your calender without using google synchronization services.
Using this you can import all calendar events to iCalendar files and also export those events back to calendar. Using Calendar Provider you can fetch all calendar data, but requires android API level 14 or above while using iCal Import Export you can fetch all calendar data for lower API levels too.
manifest
<uses-permission android:name="android.permission.READ_CALENDAR" />
Code:
Cursor cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
cursor.moveToFirst();
String[] CalNames = new String[cursor.getCount()];
int[] CalIds = new int[cursor.getCount()];
for (int i = 0; i < CalNames.length; i++) {
CalIds[i] = cursor.getInt(0);
CalNames[i] = cursor.getString(1);
cursor.moveToNext();
}
cursor.close();
Fetching all events, and particular event is done by specifying range
ContentResolver contentResolver = getContentResolver();
Uri.Builder builder = Uri.parse(getCalendarUriBase() + "/instances/when").buildUpon();
long now = new Date().getTime();
ContentUris.appendId(builder, now - DateUtils.MILLIS_PER_DAY*10000);
ContentUris.appendId(builder, now + DateUtils.MILLIS_PER_DAY * 10000);
and then let's say you wish to log events ID from calendar with ID = 1
Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { "event_id"}, "Calendars._id=" + 1,
null, "startDay ASC, startMinute ASC");
// For a full list of available columns see http://tinyurl.com/yfbg76w
while (eventCursor.moveToNext()) {
String uid2 = eventCursor.getString(0);
Log.v("eventID : ", uid2);
}
Now display this event in Listview select one of this for send as a text messsage and on receiver side :
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", HERE MSG WHICH YOU RECEIVE);
startActivity(intent);
There is no library AFAIK and it's really just a matter of retrieving the calender event and transforming it's data into XML. Unfortunately there is also no XEP on how calendar events should be represented in XMPP/XML so you have to come up with your own representation.
iCalendar/vCalendar is the calendar file format similar to vCard
You can add new field in vcard that save calender info example
VCard vCard = new VCard();
vCard.setField("calender","calender info");
calender info should be a sting, it can be a json format string.
user who receives it can load your vcard and use method
vCard.load(connection, Jid);
String string=vCard.getField("calender");
You really don't need any specific library for that purpose.
You can check out Calendar Provider
They have an awesome example on it too!
Be sure to use these permissions in your manifest.xml
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
And if you wish to send contacts then check this out Android: How to import contact from phone?
Vote this up if it helped you! I really need it. :)
i'm trying to create a calendar on my account to fill with events that i get from some websites. I've searched and found some new android 4.0 calendar example that i've modified to obtain what i need. The problem is that the calendar is created, filled with events but not synced with google calendar, so in the next sync it is erased. The funcion i use are these:
This is the one for add the new calendar if don't alreay exist:
public static Uri createCalendarWithName(Context ctx, String name,String accountName) {
Uri target = Uri.parse(CalendarContract.Calendars.CONTENT_URI.toString());
target = target.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, accountName)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google").build();
ContentValues values = new ContentValues();
values.put(Calendars.ACCOUNT_NAME, accountName);
values.put(Calendars.ACCOUNT_TYPE, "com.google");
values.put(Calendars.NAME, name);
values.put(Calendars.CALENDAR_DISPLAY_NAME, name);
values.put(Calendars.CALENDAR_COLOR, 0x00FF00);
values.put(Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_ROOT);
values.put(Calendars.OWNER_ACCOUNT, accountName);
values.put(Calendars.VISIBLE, 1);
values.put(Calendars.SYNC_EVENTS, 1);
values.put(Calendars.CALENDAR_TIME_ZONE, "Europe/Rome");
values.put(Calendars.CAN_PARTIALLY_UPDATE, 1);
values.put(Calendars.CAL_SYNC1, "https://www.google.com/calendar/feeds/" + accountName + "/private/full");
values.put(Calendars.CAL_SYNC2, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + accountName);
values.put(Calendars.CAL_SYNC3, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + accountName);
values.put(Calendars.CAL_SYNC4, 1);
values.put(Calendars.CAL_SYNC5, 0);
values.put(Calendars.CAL_SYNC8, System.currentTimeMillis());
Uri newCalendar = ctx.getContentResolver().insert(target, values);
return newCalendar;
}
and that one create the new event without interaction:
public static Uri createEventWithName(Context ctx, long id, String name, String data) {
long startMillis = 0;
long endMillis = 0;
int id2=(int)id;
String[] divisi = data.split("/");
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012,Integer.parseInt(divisi[0])-1, Integer.parseInt(divisi[1]));
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012,Integer.parseInt(divisi[0])-1, Integer.parseInt(divisi[1]));
endMillis = endTime.getTimeInMillis();
ContentValues cv = new ContentValues();
cv.put(Events.TITLE, name);
cv.put(Events.DTSTART, startMillis);
cv.put(Events.DTEND, endMillis);
cv.put(Events.CALENDAR_ID, id2);
Log.d("aggiungo a calendario",Integer.toString(id2));
cv.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().toString());
//cv.put(Events.RRULE, "FREQ=DAILY;INTERVAL=2");
Uri newEvent = ctx.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, cv);
return newEvent;
}
I'm not so experienced in Android programming so i think it's a stupid question =) i've read that accountName and Account Type must be the same that the one stored on android device, else the event is cancelled. I get accountName from android api and i think they are correct. The account type seems to work for other....
Thanks to anybody that help me!
Not possible for now.
After a lot of googling I could not find a way to create new syncable calendar in Gmail account (account type "com.google"). I've tried these scenarios:
1. without SyncAdapter and I get the same behaviour - calendar appears in external Google Calendar app with all events I added but shortly after (e.g. after couple of seconds) all events and calendar disappear.
2. with SyncAdapter and com.google authenticator - I get an error during the process
3. with SyncAdapter and com.example authenticator - possible to create calendar but not in Gmail account
Note you can create new account and create new calendar for that account (using SyncAdapter, scenario 3) but it's not a Gmail account so calendar is not syncable with Gmail Calendar (e.g. if you login to Gmail account in web browser and open Google Calendar that calendar you've just created will not show).
from http://developer.android.com/reference/android/provider/CalendarContract.html
public static final String ACCOUNT_TYPE_LOCAL
Added in API level 14 A special account type for calendars not
associated with any account. Normally calendars that do not match an
account on the device will be removed. Setting the account_type on a
calendar to this will prevent it from being wiped if it does not match
an existing account.
See Also ACCOUNT_TYPE Constant Value: "LOCAL"
Same issue i was facing. After making the below change now i am seeing the events permanently sticking to my calendar.
In the addEvent method add the below line
Uri uri = getContentResolver().insert(asSyncAdapter(Events.CONTENT_URI,"abc#gmail.com","com.gmail"), cv);
Code for asSyncAdapter is as below
static Uri asSyncAdapter(Uri uri, String account, String accountType) {
return uri.buildUpon()
.appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true")
.appendQueryParameter(Calendars.ACCOUNT_NAME, account)
.appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build();
}
According to the Javadoc for CalendarContract.Calendars:
"Calendars are designed to be primarily managed by a sync adapter and inserting new calendars should be done as a sync adapter."
If you are not a sync adapter (if you are just an arbitrary app, you are not a sync adapter), that may explain why your new calendar is not getting synchronized like you would expect.