I am unable to get recurring events from google calandar. Can any one help me with this.
I have also used "Instances" table to get the recurring events but not. Can any one guide me how to fetch data from "Instances" table. It should be a great help for me.
I used following code to read events.
Uri uri = CalendarContract.Events.CONTENT_URI;
String[] projection = new String[]{
CalendarContract.Events._ID,
CalendarContract.Events.DTSTART,
CalendarContract.Events.DTEND,
CalendarContract.Events.ALL_DAY,
CalendarContract.Events.TITLE,
CalendarContract.Events.EVENT_COLOR,
CalendarContract.Events.CALENDAR_ID,
CalendarContract.Events.DESCRIPTION,
CalendarContract.Events.ORGANIZER,
// If status = 2 it is cancelled by organizer
CalendarContract.Events.STATUS,
// If self attendee status = 2 it is declined by user
CalendarContract.Events.SELF_ATTENDEE_STATUS,
CalendarContract.Events.RDATE,
CalendarContract.Events.LAST_DATE
};
String[] calendarID;
for (int i = 0; i < EmailId.size(); i++) {
calendarID = new String[]{EmailId.get(i)};
Cursor c = context.getContentResolver().query(uri, projection,
CalendarContract.Instances.CALENDAR_ID + " = ?", calendarID, CalendarContract.Events.DTSTART);
if (c.moveToFirst()) {
do {
Evnt e = new Evnt();
e._id = c.getLong(0);
e.startDate = "";
e.startDate = getDate(c.getString(1));
e.startTime = "";
e.startTime = getTime(c.getString(1));
e.endDate = "";
e.endDate = getDate(c.getString(2));
e.endTime = "";
e.endTime = getTime(c.getString(2));
e.all_day = c.getInt(3);
e.title = c.getString(4);
e.color = c.getInt(5);
e.calendar_id = c.getLong(6);
e.desc = c.getString(7);
e.organizer = c.getString(8);
e.nine = c.getString(9);
e.ten = c.getString(10);
e.eleven = c.getString(11);
e.twelve = "";
e.twelve = getDate(c.getString(12));
ourEvents.add(e);
} while (c.moveToNext());
}
Related
After my android app sync event to google calendar and i see color google android calendar but doesn't change google web calendar event color. What i to do wrong?
Name is accountName.
protected void fetchCalendarForUser(String name) {
Cursor cur = null;
ContentResolver cr = getContentResolver();
Uri uri = CalendarContract.Calendars.CONTENT_URI;
String selection = "((" + CalendarContract.Calendars.ACCOUNT_NAME + " = ?) AND ("
+ CalendarContract.Calendars.ACCOUNT_TYPE + " = ?) AND ("
+ CalendarContract.Calendars.OWNER_ACCOUNT + " = ?))";
String[] selectionArgs = new String[] {name, "com.google", name};
cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);
cur.moveToNext();
long calID = 0;
String displayName = null;
String accountName = null;
String ownerName = null;
// Get the field values
calID = cur.getLong(PROJECTION_ID_INDEX);
displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX);
accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX);
ownerName = cur.getString(PROJECTION_OWNER_ACCOUNT_INDEX);
int i = 0;
List<Date> laststart = new ArrayList<Date>();
List<Date> lastend = new ArrayList<Date>();
boolean equal=false;
for (Map.Entry<String, ArrayList<CalendarEntry>> item : calendarEntriesByDay.entrySet()) {
for (CalendarEntry entry : item.getValue()) {
for(int j=0;j<laststart.size();j++)
{
if(laststart.get(j) == entry.getStart()){
if(lastend.get(j) == entry.getEnd()){
equal=true;
}
}
}
if(equal==false) {
createEventOnCalendar(calID, entry);
i++;
}
else
{
equal=false;
}
laststart.add(entry.getStart());
lastend.add(entry.getEnd());
}
}
if (i > 0) {
Crouton.makeText(this, getResources().getString(eu.rerisoft.servicebus.R.string.google_calendar_add_success, displayName, String.valueOf(i)), Style.ALERT).show();
} else {
Crouton.makeText(this, eu.rerisoft.servicebus.R.string.google_calendar_add_error, Style.ALERT).show();
}
}
protected long createEventOnCalendar(long calID, CalendarEntry event) {
String colorString = getResources().getString(event.getColorRes());
int eventColor = Color.parseColor(colorString);
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, event.getStart().getTime());
values.put(CalendarContract.Events.DTEND, event.getEnd().getTime());
values.put(CalendarContract.Events.TITLE, event.getTitle());
values.put(CalendarContract.Events.EVENT_COLOR, eventColor);
values.put(CalendarContract.Events.DESCRIPTION, event.getDescription());
values.put(CalendarContract.Events.CALENDAR_ID, calID);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getTimeZone("UTC").getDisplayName());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
long eventID = Long.parseLong(uri.getLastPathSegment());
return eventID;
}
Sorry for bad english.
I have done same thing before 3 months:
String appointmentColorCode = "Color_which_Is_Currently_Getting_From_Calendar";
int mColorCode = 0;
mColorCode = (0xff000000 + Integer.parseInt(appointmentColorCode));
You have to give Color_which_Is_Currently_Getting_From_Calendar as you are getting from calendar.
mColorCode will be your final Color code.'
Done
I found this article very useful http://developer.android.com/guide/topics/providers/calendar-provider.html#query?
But here i found the ACCOUNT_NAME queried in the calendar is being written statically
I am stuck here Since my problem is ..what if i want user's gmail address through some query
i.e not statically specifying the address rather getting it through some code
PS: i am very newbie to android. Apologies if this ones a stupid question
long calID = 0;
String displayName = null;
String accountName = null;
String ownerName = null;
final String[] EVENT_PROJECTION = new String[] {
Calendars._ID, // 0
Calendars.ACCOUNT_NAME, // 1
Calendars.CALENDAR_DISPLAY_NAME, // 2
Calendars.OWNER_ACCOUNT // 3
};
// The indices for the projection array above.
final int PROJECTION_ID_INDEX = 0;
final int PROJECTION_ACCOUNT_NAME_INDEX = 1;
final int PROJECTION_DISPLAY_NAME_INDEX = 2;
final int PROJECTION_OWNER_ACCOUNT_INDEX = 3;
// Run query
Cursor cur = null;
ContentResolver cr = app.getContentResolver();
Uri uri = Calendars.CONTENT_URI;
String selection = "((" + Calendars.ACCOUNT_NAME + " = ?) AND ("
+ Calendars.ACCOUNT_TYPE + " = ?) AND ("
+ Calendars.OWNER_ACCOUNT + " = ?))";
String[] selectionArgs = new String[] { "sample#gmail.com",
"com.google", "sample#gmail.com" };
// Submit the query and get a Cursor object back.
cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs,
null);
// Use the cursor to step through the returned records
while (cur.moveToNext()) {
// Get the field values
calID = cur.getLong(PROJECTION_ID_INDEX);
displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX);
accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX);
ownerName = cur.getString(PROJECTION_OWNER_ACCOUNT_INDEX);
}
Calendar beginTime = Calendar.getInstance();
Calendar ceaseTime = Calendar.getInstance();
ContentValues cv = new ContentValues();
HashMap<String, String> map = (HashMap<String, String>) params[0];
String startDate[] = map.get("startDate").toString().split("-");
String startTime[] = map.get("startTime").toString().split(":");
long beginMilis=0,ceaseMilis=0;
beginTime.set(Integer.parseInt(startDate[2]),
Integer.parseInt(startDate[1]),
Integer.parseInt(startDate[0]),
Integer.parseInt(startTime[0]),
Integer.parseInt(startTime[1]));
beginMilis = beginTime.getTimeInMillis();
String endDate[] = map.get("endDate").toString().split("-");
String endTime[] = map.get("endTime").toString().split(":");
TimeZone tz = TimeZone.getDefault();
ceaseTime.set(Integer.parseInt(endDate[2]),
Integer.parseInt(endDate[1]),
Integer.parseInt(endDate[0]),
Integer.parseInt(endTime[0]),
Integer.parseInt(endTime[1]));
ceaseMilis = ceaseTime.getTimeInMillis();
cv.put(Events.DTSTART, beginMilis);
cv.put(Events.DTEND, ceaseMilis);
cv.put(Events.TITLE, map.get("title").toString());
cv.put(Events.DESCRIPTION, map.get("description").toString());
cv.put(Events.CALENDAR_ID, String.valueOf(calID));
cv.put(Events.EVENT_TIMEZONE, tz.getDisplayName());
uri = cr.insert(Events.CONTENT_URI, cv);
First of all I'm sorry I don't speak very good english. I'm currently developing an android app with eclipse and I have problem while fetching data from database. This is the method to read data from database. I put this method in a class named SQLiteAdapter:
public Cursor getQuestionEachLevel(long levelId)
{
return sqLiteDatabase.query(MYDATABASE_TABLE, new String[]{KEY_ROWID, KEY_ANSWER,
KEY_QUESTION, KEY_HINT, KEY_FLAG, KEY_LEVEL}, KEY_LEVEL + "=" + levelId,
null, null, null, null, null);
}
And this is the code that going to call that method and fetch the data. I put this code in another class.
int x=0;
String getId[] = null;
String getAnswer[] = null;
String getQuestion[] = null;
String getHint[] = null;
String getFlag[]= null;
String getLevel[]= null;
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor c = mySQLiteAdapter.getQuestionEachLevel(getLevelKey);
if(c.moveToFirst()){
do{
getId[x] = c.getString(0);
getAnswer[x] = c.getString(1);
getQuestion[x] = c.getString(2);
getHint[x] = c.getString(3);
getFlag[x] = c.getString(4);
getLevel[x] = c.getString(5);
x++;
}while(c.moveToNext());
}
mySQLiteAdapter.close();
I want to put each of the data from the database column into specific Variable because I'm going to use it later. Eclipse doesn't show any error but when I try to run this app on my phone, the app crash while fetching the data. Anyone know what's wrong with this?
Post your logcat, by all means. But in the meantime
int x=0;
String getId[] = null;
String getAnswer[] = null;
String getQuestion[] = null;
String getHint[] = null;
String getFlag[]= null;
String getLevel[]= null;
and afterwards
do{
getId[x] = c.getString(0);
getAnswer[x] = c.getString(1);
getQuestion[x] = c.getString(2);
getHint[x] = c.getString(3);
getFlag[x] = c.getString(4);
getLevel[x] = c.getString(5);
x++;
}while(c.moveToNext());
is always pretty problematic... (your arrays aren't initialized and you're attempting to access them. I'd advise this change:
int x=0;
List<String> getId = new ArrayList<String>();
List<String> getAnswer = new ArrayList<String>();
List<String>getQuestion = new ArrayList<String>();
List<String> getHint = new ArrayList<String>();
List<String>getFlag= new ArrayList<String>();
List<String>getLevel = new ArrayList<String>();
and afterwards
do{
getId.add( c.getString(0));
getAnswer.add( c.getString(1));
getQuestion.add(c.getString(2));
getHint.add(c.getString(3));
getFlag.add(c.getString(4));
getLevel.add(c.getString(5));
x++;
}while(c.moveToNext());
You must initialize all the arrays before using it.
int x=0;
String getId[] = null;
String getAnswer[] = null;
String getQuestion[] = null;
String getHint[] = null;
String getFlag[]= null;
String getLevel[]= null;
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor c = mySQLiteAdapter.getQuestionEachLevel(getLevelKey);
if(c.moveToFirst()){
int numOfRows = c.getCount();
getId = new String[numOfRows];
getAnswer = new String[numOfRows];
getQuestion = new String[numOfRows];
getHint = new String[numOfRows];
getFlag = new String[numOfRows];
getLevel = new String[numOfRows];
do{
getId[x] = c.getString(0);
getAnswer[x] = c.getString(1);
getQuestion[x] = c.getString(2);
getHint[x] = c.getString(3);
getFlag[x] = c.getString(4);
getLevel[x] = c.getString(5);
x++;
}while(c.moveToNext());
}
mySQLiteAdapter.close();
Having trouble in showing the inbox message in android. I have used two contentresolver in one activity . is it going to effect my program? Here is my code:
ContentResolver contentResolver = getContentResolver();
ContentResolver contentResolverSender = getContentResolver();
Cursor cursorsender = contentResolverSender.query(
Uri.parse("content://sms/sent"), null, "address = "
+ senderAddress, null, null);
Cursor cursor = contentResolver.query(Uri.parse("content://sms/inbox"),
null, "address = " + senderAddress, null, null);
startManagingCursor(cursor);
int indexBody = cursor.getColumnIndex(SmsReceiver.BODY);
int indexAddr = cursor.getColumnIndex(SmsReceiver.ADDRESS);
int dateCol = cursor.getColumnIndex(SmsReceiver.DATE);
int i = 0, flag = 0;
int indexSenderBody = cursorsender.getColumnIndex(SmsReceiver.BODY);
String[] addressUniqueness = null;
String name = null;
if (indexBody < 0 || !cursor.moveToFirst())
return;
int j = 0;
String msgSendStr = null;
if (cursorsender.moveToFirst()) {
for (i = 0; i < cursorsender.getCount(); i++) {
msgSendStr = cursorsender.getString(
cursorsender.getColumnIndexOrThrow("body")).toString();
msgSendStr = makeShortMstToText(msgSendStr);
bodySendMsg[i] = msgSendStr;
long dateInMilliSecSender = cursorsender.getLong(cursorsender
.getColumnIndexOrThrow(SmsReceiver.DATE));
dateSendMsg[i] = dateInMilliSecSender;
cursorsender.moveToNext();
}
}
if (cursor.moveToFirst()) {
do {
String msgStr = cursor.getString(indexBody);
long dateInMilliSec = cursor.getLong(cursor
.getColumnIndexOrThrow(SmsReceiver.DATE));
msgStr = makeShortMstToText(msgStr);
bodyRcvMsg[j] = msgStr;
dateRcvMsg[j] = dateInMilliSec;
j++;
} while (cursor.moveToNext());
// TODO Auto-generated catch block
}
whenever i open my inbox failed to show inbox message though there is message in my inbox.It shows that start conversation .
I'm trying to create a "select-multiple" contact list where I can allow a user to check more than one contact. What I'm looking for is effectively the same thing as the native activity that appears when composing a message to multiple contacts. Thanks!
At first you need to get a list of contacts and then display it on ListView element. For example I'm using following code to display all users contacts on a ListView:
// Run query on all contacts id
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
String selection = null;//ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '" + ("1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
ContentResolver contectResolver = getContentResolver();
Cursor cursor = contectResolver.query(uri, projection, selection, selectionArgs,
sortOrder);
//Create buffer
final ArrayList<ContactData> bufferContacts = new ArrayList<ContactData>();
//Load contacts one by one
if(cursor.moveToFirst()) {
while(!cursor.isAfterLast()) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String[] emailProj = new String[]{Email.DATA};
Cursor cursorEmail = contectResolver.query(Email.CONTENT_URI, emailProj,Email.CONTACT_ID + " = ? ", new String[] { id }, null);
String[] phoneProj = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
Cursor cursorPhone = contectResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, phoneProj,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
String firstName = "";
String lastName = "";
String email = "";
String displayname = "";
String phoneNmb = "";
if(cursorPhone.moveToFirst()) {
///displayname = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNmb = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursorPhone.close();
if(cursorEmail.moveToFirst()) {
email = cursorEmail.getString(cursorEmail.getColumnIndex(Email.DATA));
}
cursorEmail.close();
displayname = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//Divide display name to first and last
String[] names = displayname.split("\\s+");
firstName = displayname;
if(names.length >= 1) {
firstName = names[0];
}
if(names.length >= 2) {
firstName = names[1];
}
final ContactData contactData = new ContactData(id, firstName, lastName, phoneNmb, email, allChecked);
bufferContacts.add(contactData);
//Set list view initialy
runOnUiThread(new Runnable() {
public void run() {
if(contactsAdapter == null) {
ArrayList<ContactData> contacts = new ArrayList<ContactData>();
contactsAdapter = new ContactAdapter(ContactPickerActivity.this, contacts);
lvContacts.setAdapter(contactsAdapter);
}
if(bufferContacts.size() >= BUFFER_INTERVAL) {
addBuffer(bufferContacts);
}
}
});
cursor.moveToNext();
}
}
cursor.close();
runOnUiThread(new Runnable() {
public void run() {
addBuffer(bufferContacts);
}
});
If you don't want to bother yourself with making everything out of scratch then feel free to use my ready library for selecting multiple contacts:
https://github.com/kgadzinowski/Android-Multiple-Contacts-Picker-Library