(first of all, sorry if any of this sounds stupid, but I'm a total nood regarding android)
I'm working on an organizer app where I want to create a Calendar Event using an intend, and afterwards I have to save the events ID in my app so I can open it up again to edit it and so my app can read the events start date.
the code I'm using, I got from this question:
How can I find out the result of my calendar intent?
The intend works but I can't get the rest to work. also, "EventUtility" and "mContext" show up as errors.
p.s. I also get a warning about a missing Permission but I have no idea where to add that
protected void kalender() {
long event_id = EventUtility.getNewEventId(mContext.getContentResolver());
Intent newEvent = new Intent(Intent.ACTION_INSERT)
.setData(CalendarContract.Events.CONTENT_URI)
.putExtra(CalendarContract.Events.TITLE, "Abgabe Termin")
.putExtra(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_BUSY)
.putExtra(CalendarContract.Events.HAS_ALARM, true)
.putExtra("allDay", true);
startActivity(newEvent);
}
public static long getNewEventId(ContentResolver cr) {
Cursor cursor = cr.query(CalendarContract.Events.CONTENT_URI, new String[]{"MAX(_id) as max_id"}, null, null, "_id");
cursor.moveToFirst();
long max_val = cursor.getLong(cursor.getColumnIndex("max_id"));
return max_val + 1;
}
Related
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 want to start an activity once i get a missed call.but iam unable to read the latest missed call entry from the call log, instead it reads the entry before the current one. iam reading it when phone state is idle.
Example: say there are two missed call entries one at 11:10 and other at 11:11. I get a missed call at 12:12, my activity needs to show missed calls obtained at 12:12,11:11,11:10. but rather it reads 11:11,11:10. Latest entry is missing. what should i do? I am using a service and reading call log, phone state.
Cursor c = getContentResolver().query(allCalls, null, null, null, order);
if (c.moveToFirst()) {
do{
// getting number,type,ack etc}
while (c.moveToNext()); }
You will need to listen for it using TelephonyManager and PhoneStateListener.
This code will show you how to do it:
if(lastStart == TelephonyManager.CALL_STATE_RINGING)
{
long now = System.currentTimeMillis();
long duration = (now-ringStartTime)/1000;
bindService.AddMissCallInfo(ringPhoneNumber, ringStartTime, duration);
Log.i(Constants.Tag,"service add miss call!");
}
Code taken from: http://code.google.com/p/android-miss-call/source/browse/trunk/src/com/call/DialPhoneStateListener.java?r=3
you can used like this.
c=mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, CallLog.Calls._ID + " DESC");
if(c.getCount()!=0){
c.moveToFirst();
lastCallnumber = c.getString(0);
lastCallnumber=FormatNumber(lastCallnumber);
String type=c.getString(1);
String duration=c.getString(2);
String name=c.getString(3);
String id=c.getString(4);
System.out.println("CALLLLing:"+lastCallnumber+"Type:"+type+"\t id:"+id);
if(type.equals("3")){
//you can get last missedcall here
}
You can try it may be helpful for you.
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.
I'm attempting to update a calendar's event on my phone from my code, but context.getContentResolver().update keeps returning 0, and of course there are no changes made to the event when I look at it in the Calendar app.
I'm getting the event ID, start time, etc with context.getContentResolver().query, and I'm getting unique numbers like 431, 4, 233, etc, so I'm presuming the event IDs I'm using are real.
I understand the official way to do this is to go through Google's servers instead of using update(), but for my implementation it doesn't make sense to do it that way (or even in general, but I digress).
Am I doing something wrong, or am I trying to do something that Android simply isn't going to allow?
Uri updateEventUri = ContentUris.withAppendedId(Uri.parse("content://com.android.calendar/events"), id);
ContentValues cv = new ContentValues();
begin.set(Calendar.HOUR_OF_DAY, arg0.getCurrentHour()); //begin is a java.util.Calendar object
begin.set(Calendar.MINUTE, arg0.getCurrentMinute());
//cv.put("_id", id);
//cv.put("title", "yeahyeahyeah!");
cv.put("dtstart", begin.getTimeInMillis());
int updatedrowcount = context.getContentResolver().update(updateEventUri, cv, null, null);
System.out.println("updated "+updatedrowcount+" rows with id "+id);
A related question was posted here with no replies https://stackoverflow.com/questions/5636350/update-android-calendar-event
Let me know if I can clarify anything; I would really appreciate any input you guys and dolls could provide!
i had tried a lot and finally ended up with solution (Unreliable though).. but works fine..
public static boolean updateCalendar(Context context,String cal_Id,String eventId)
{
try{
Uri CALENDAR_URI = Uri.parse(CAL_URI+"events");
Cursor c = context.getContentResolver().query(CALENDAR_URI, null, null, null, null);
String[] s = c.getColumnNames();
if (c.moveToFirst())
{
while (c.moveToNext())
{
String _id = c.getString(c.getColumnIndex("_id"));
String CalId = c.getString(c.getColumnIndex("calendar_id"));
if ((_id==null) && (CalId == null))
{
return false;
}
else
{
if (_id.equals(eventId) && CalId.equals(cal_Id))
{
Uri uri = ContentUris.withAppendedId(CALENDAR_URI, Integer.parseInt(_id));
context.getContentResolver().update(uri, null, null, null);// need to give your data here
return true;
}
}
}
}
}
finally
{
return true;
}
}
and finally i'm not sure if it works with every device.
Ok, so, the problem was that I was using different URIs between fetching the events and editing them. I used the code sample from here and was using the URI "content://com.android.calendar/instances/when" to fetch the events and display them on the screen. When I had made a change I was using "content://com.android.calendar/events" to edit by id as in my example above.
What I found, thanks to your response, ntc, was that the ids for events between the two URIs were different, and therefore I couldn't edit the events consistently with the information each was giving me. I was presuming the event ids I was getting were system ids and universal to the phone.
I guess I'll have to do some testing and see what hardware isn't compatible with this method. I am using an HTC Evo for testing and so far so good.
When querying the Instances table, use Instances.EVENT_ID to get the identifier for the event you want to edit, instead of Instances._ID.
Is it possible to get the calendar's entries from the phone offline? It seem the only way is to use gdata-java-client.
Josef and Isaac's solutions for accessing the calendar only work in Android 2.1 and earlier. Google have changed the base content URI in 2.2 from "content://calendar" to "content://com.android.calendar". This change means the best approach is to attempt to obtain a cursor using the old base URI, and if the returned cursor is null, then try the new base URI.
Please note that I got this approach from the open source test code that Shane Conder and Lauren Darcey provide with their Working With The Android Calendar article.
private final static String BASE_CALENDAR_URI_PRE_2_2 = "content://calendar";
private final static String BASE_CALENDAR_URI_2_2 = "content://com.android.calendar";
/*
* Determines if we need to use a pre 2.2 calendar Uri, or a 2.2 calendar Uri, and returns the base Uri
*/
private String getCalendarUriBase() {
Uri calendars = Uri.parse(BASE_CALENDAR_URI_PRE_2_2 + "/calendars");
try {
Cursor managedCursor = managedQuery(calendars, null, null, null, null);
if (managedCursor != null) {
return BASE_CALENDAR_URI_PRE_2_2;
}
else {
calendars = Uri.parse(BASE_CALENDAR_URI_2_2 + "/calendars");
managedCursor = managedQuery(calendars, null, null, null, null);
if (managedCursor != null) {
return BASE_CALENDAR_URI_2_2;
}
}
} catch (Exception e) { /* eat any exceptions */ }
return null; // No working calendar URI found
}
These answers are good, but they all involve hard-coding the Calendar URI (which I've seen in three different incarnations across different Android devices).
A better way to get that URI (which hard-codes the name of a class and a field instead) would be something like this:
Class<?> calendarProviderClass = Class.forName("android.provider.Calendar");
Field uriField = calendarProviderClass.getField("CONTENT_URI");
Uri calendarUri = (Uri) uriField.get(null);
This isn't perfect (it will break if they ever remove the android.provider.Calendar class or the CONTENT_URI field) but it works on more platforms than any single URI hard-code.
Note that these reflection methods will throw exceptions which will need to be caught or re-thrown by the calling method.
Currently, this is not possible without using private APIs (see Josef's post.) There is a Calendar provider, but it is not public yet. It could change anytime and break your app.
Though, it probably will not change (I don't think they will change it from "calendar"), so you might be able to use it. But my recommendation is to use a separate class like this:
public class CalendarProvider {
public static final Uri CONTENT_URI = Uri.parse("content://calendar");
public static final String TITLE = "title";
public static final String ....
And use those instead of the strings directly. This will let you change it very easily if/when the API changes or it is made public.
You can use the calendar content provider (com.android.providers.calendar.CalendarProvider). Example:
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(Uri.parse("content://calendar/events"), null, null, null, null);
while(cursor.moveToNext()) {
String eventTitle = cursor.getString(cursor.getColumnIndex("title"));
Date eventStart = new Date(cursor.getLong(cursor.getColumnIndex("dtstart")));
// etc.
}
edit: you might want to put this in a wrapper (see Isaac's post) as it's currently a private API.
You can use the CalendarContract from here: https://github.com/dschuermann/android-calendar-compatibility
It is the same API class as available on Android 4, but made to work with Android >= 2.2.
About the API that can change... The whole ContentProvider approach won't change that quickly so can already overcome a lot of problems by only updating the strings. Therefor create constants you reuse over the whole project.
public static final String URI_CONTENT_CALENDAR_EVENTS = "content://calendar/events";
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(Uri.parse(URI_CONTENT_CALENDAR_EVENTS), null, null, null, null);
//etc
If you want a proper private API you'll have to create a pojo and some services like this:
public class CalendarEvent {
private long id;
private long date;
//etc...
}
public interface CalendarService {
public Set<CalendarEvent> getAllCalendarEvents();
public CalendarEvent findCalendarEventById(long id);
public CalendarEvent findCalendarEventByDate(long date);
}
and so on. This way you'll only have to update the CalendarEvent object and this service in case the API changes.
Nick's solution involves managedQuery, which is not defined in the Context class. Many times when you are running things in the background you would want to use a context object. Here's a modified version:
public String getCalendarUriBase() {
return (android.os.Build.VERSION.SDK_INT>=8)?
"content://com.android.calendar":
"content://calendar";
}
The catch for null should not be carried out here since there might be more exceptions even if the managedQuery succeeded earlier.