android - How to display local calendar? - android

I'm using the new Calendar API and I want to create a local calendar in my application on which the user can add custom events related to the app .. So I've been able to create it through this code:
/** Creates the values the new calendar will have */
private static ContentValues buildNewCalContentValues() {
final ContentValues cv = new ContentValues();
cv.put(Calendars.ACCOUNT_NAME, ACCOUNT_NAME);
cv.put(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
cv.put(Calendars.NAME, CALENDAR_NAME);
cv.put(Calendars.CALENDAR_DISPLAY_NAME, CALENDAR_NAME);
cv.put(Calendars.CALENDAR_COLOR, 0xEA8561);
// user can only read the calendar
cv.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_READ);
cv.put(Calendars.OWNER_ACCOUNT, ACCOUNT_NAME);
cv.put(Calendars.VISIBLE, 1);
cv.put(Calendars.SYNC_EVENTS, 1);
return cv;
}
//Create and insert new calendar into android database
public static void createCalendar(Context ctx) {
ContentResolver cr = ctx.getContentResolver();
final ContentValues cv = buildNewCalContentValues();
Uri calUri = buildCalUri();
// insert the calendar into the database
cr.insert(calUri, cv);
}
But the problem is I don't know how to display it and make the user interact with it.
Any help? Thanks in advance :)

Related

Progrommatically created Calendar not saved

I'm trying to create calendar by using code from this topic
private static void createCalendar( Context mContext, Account account)
{
final ContentValues v = new ContentValues();
v.put(CalendarContract.Calendars.NAME,"TEST");
v.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "TEST");
v.put(CalendarContract.Calendars.ACCOUNT_NAME, account.name);
v.put(CalendarContract.Calendars.ACCOUNT_TYPE, account.type);
v.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.GREEN);
v.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
v.put(CalendarContract.Calendars.OWNER_ACCOUNT, account.name);
// v.put(CalendarContract.Calendars._ID, 123);
v.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
v.put(CalendarContract.Calendars.VISIBLE, 1);
Uri creationUri = asSyncAdapter(CalendarContract.Calendars.CONTENT_URI, account.name, account.type);
Uri calendarData = mContext.getContentResolver().insert(creationUri, v);
long id = Long.parseLong(calendarData.getLastPathSegment());
}
private static Uri asSyncAdapter(Uri uri, String account, String accountType)
{
return uri.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true").appendQueryParameter
(CalendarContract.Calendars.ACCOUNT_NAME,account)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, accountType) .build();
}
and then make a call:
createCalendar(c, AccountManager.get(c).getAccountsByType("com.google")[0]); // i've checked - it's correct account to add calendar
Then i retrive data from ContentResolver
final ContentResolver cr = ctx.getContentResolver();
Cursor cursor ;
cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"),null, null, null, null);
and see new calendar in cursor. But:
it not appears in google accound
it removes after some time (guess, because of synchronization)
tested on 5.1.1
How can I create calendar to my account progrommatically?
Try using calendar.insert to create another calendar.
Since you're using Java, I think this code snippet might be of help:
import com.google.api.services.calendar.Calendar;
// ...
// Initialize Calendar service with valid OAuth credentials
Calendar service = new Calendar.Builder(httpTransport, jsonFactory, credentials)
.setApplicationName("applicationName").build();
// Create a new calendar
com.google.api.services.calendar.model.Calendar calendar = new Calendar();
calendar.setSummary("calendarSummary");
calendar.setTimeZone("America/Los_Angeles");
// Insert the new calendar
Calendar createdCalendar = service.calendars().insert(calendar).execute();
System.out.println(createdCalendar.getId());
Give the Try-it a dry run to see if it works.

Remove and Update existing Calendar Event

I am following this gist, to insert event into Calendar
How do I update existing Calendar Event, which i have inserted earlier using below code:
public void addToCalender() throws ParseException {
......
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.CALENDAR_ID, calendarId[0]);
event.put(CalendarContract.Events.TITLE, "Event Title");
event.put(CalendarContract.Events.DESCRIPTION, "Event Description");
event.put(CalendarContract.Events.EVENT_LOCATION, "Eevnt Location");
event.put(CalendarContract.Events.DTSTART, startCalTime);
event.put(CalendarContract.Events.DTEND, endCalTime);
event.put(CalendarContract.Events.STATUS, 1);
event.put(CalendarContract.Events.HAS_ALARM, 1);
event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
Uri insertEventUri = AddEventActivity.this.getContentResolver().insert(
eventsUri, event);
ContentValues reminders = new ContentValues();
reminders.put(Reminders.EVENT_ID,
Long.parseLong(insertEventUri.getLastPathSegment()));
reminders.put(Reminders.METHOD, Reminders.METHOD_ALERT);
reminders.put(Reminders.MINUTES, 10);
AddEventActivity.this.getContentResolver().insert(remainderUri, reminders);
}
I would like to know, How do I :
1. Remove existing event
2. Update existing event
Here is how you can modify an event. Let's say its ID is eventID:
public void updateEvent(int eventID)
{
ContentResolver cr = context.getContentResolver();
Uri eventUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.TITLE, "new title");
event.put(CalendarContract.Events.DESCRIPTION, "My cool event!");
cr.update(eventUri, event, null, null);
}
To remove an event, you have two possibilities:
If you are a standard application:
Use the previous code and replace all the event.put by this one:
event.put(CalendarContract.Events.DELETED, 1);
If you are the SyncAdapter of the calendar and want a real deletion (The previous method just says that the event should be deleted and the one that follows must be used once the calendar is being synced :
public deleteEvent(Uri eventUri)
{
cr.delete(event, null, null);
}
Where eventUri is the Uri of the event obtained as shown previously from the event ID.
For all of the previous methods, if you get an exception about not being in a sync adapter, you can use this:
public static Uri asSyncAdapter(Uri uri, String account, String accountType)
{
return uri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, account)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, accountType)
.build();
}
I hope this will help you.
Source: personnal code + http://developer.android.com/guide/topics/providers/calendar-provider.html
...
Outlook in fact seems to respond to the rules defined in RFC 2446
In summary you have to specify
METHOD:REQUEST and ORGANIZER:xxxxxxxx
...
Please read the original answer by Tom Carter.

Calendar I created on "com.google" on android erased after sync

I created a new calendar on android and added events.
After first sync or after a few seconds it's erasing. Can somebody help with this problem?
Code:
private void createCalendar(Context context, String accName) {
Uri calUri = CalendarContract.Calendars.CONTENT_URI;
final ContentValues v = new ContentValues();
v.put(CalendarContract.Calendars.ACCOUNT_NAME, accName);
v.put(CalendarContract.Calendars.NAME, CALENDAR_NAME);
v.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CALENDAR_NAME);
v.put(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google");
v.put(CalendarContract.Calendars.CALENDAR_COLOR, 0xEA8561);
v.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
v.put(CalendarContract.Calendars.OWNER_ACCOUNT, accName);
v.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
v.put(CalendarContract.Calendars.VISIBLE, 1);
calUri = calUri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, accName)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google")
.build();
calUri = calUri.buildUpon().build();
final Uri result = context.getContentResolver().insert(calUri, v);
}
Unfortinately this is a known issue with the calendar API. you have only one option if you want to use the Android calendar- create the calendar as a local one and synchronize it yourself.

how to use the last id SQLite

I am new to the android world and have a problem with an id. What i need is that when the user clicks on new match it will insert a new row into the db. This is working and i get the lastId but now i need this id in the next activities. How can i store that id so i can use it elsewhere?
This is how i insert the new match:
public void newMatch(WedstrijdenGeschiedenis wedstrijd){
// 1.
SQLiteDatabase db = this.getWritableDatabase();
// 2.
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = new Date();
ContentValues values = new ContentValues();
values.put(KEY_DATUM, dateFormat.format(date)); // get datum
// 3.
long lastId = db.insert(TABLE_WEDSTRIJD, // table
null, //nullColumnHack
values); // key/value -> keys = column names/ values = column values
Log.d("New Match","ID ="+lastId);
// 4. close
db.close();
}
so i see the lastId in LogCat but i don't know how to store it for further use. I tried void but offcourse that is not possible on void. Sorry for the dummy question
change void to long and add a return statement that returns the lastId
public long newMatch(WedstrijdenGeschiedenis wedstrijd){
// Your other code
return lastId;
}
Access it with:
long lastId= db.newMatch(new WedstrijdenGeschiedenis());

write in the android calendar

As part of a project, I created an android application which communicates with an online database (MySQL) to integrate the appointment has taken the online calendar.
I collect the data, converted to Json, but when the inscrires in the agenda of android mobile I encounter a probleme, here is my code : (sorry for my english )
EDIT :
public class calendrier extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor cursor = getContentResolver()
.query(Uri.parse("content://com.android.calendar/calendars"),
new String[] { "_id", "displayName" }, "selected=1",
null, null);
if (cursor != 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();
if (calIds.length > 0) {
// we're safe here to do any further work
}
// grab calendar id from above
int cal_id = calIds[0];
// set the content value
ContentValues cv = new ContentValues();
cv.put("calendar_id", cal_id);
cv.put("title", "titre");
cv.put("description", "bla bla bla");
cv.put("eventLocation", "city");
// note: you're going to need to convert the desired date into
// milliseconds
cv.put("dtstart", System.currentTimeMillis());
cv.put("dtend", System.currentTimeMillis()
+ DateUtils.DAY_IN_MILLIS);
cv.put("allDay", 0); // true = 1, false = 0
cv.put("hasAlarm", 1);
// once desired fields are set, insert it into the table
getContentResolver().insert(
Uri.parse("content://com.android.calendar/events"), cv);
}
}
}
this code works but it asks me if I want to participate in the event when I open it and I wish he does not do
thanks
//did U include permission in your manifest.xml file
android.permission.READ_CALENDAR
android.permission.WRITE_CALENDAR
String WRITE_CALENDAR Allows an application to write (but not read) the user's calendar data.
The Uri of Calendar has changed from Android 2.2 .
Old (2.1 and before): content://calendar/
New (2.2): content://com.android.calendar/
Change your Uri to the new one.
There is no Calendar Application by default in Android. So the Uri may not work at all.
If, you are getting the first error message as "Failed to find provider info for com.android.calendar", you need to check the handset you are using to debug uses which Calendar URI.
To insert Time in Calendar event, you can use the following code example:
Calendar calendar = Calendar.getInstance();
calendar.getTimeInMillis();
cv.put("dtstart", ""+calendar.getTimeInMillis());
cv.put("dtend", ""+calendar.getTimeInMillis()+10000);

Categories

Resources