hey I am unable to get the call type == missed call and of current date only.
Its can be achieved from Cursor query i have tried this but getting Calls type as missed call
I am using this in onCreate method but not getting any value in logcat????
Here is my piece of code any help would be greatly appreciated......thanks in advance
String where = CallLog.Calls.TYPE+"="+CallLog.Calls.MISSED_TYPE;
int missedtype=CallLog.Calls.MISSED_TYPE;
String missed=Integer.toString(missedtype);
String timestamp = String.valueOf(getTodayTimestamp());
Cursor cursor = this.getContentResolver()
.query(CallLog.Calls.CONTENT_URI,
new String[] { CallLog.Calls.DATE, CallLog.Calls.TYPE,
CallLog.Calls.DURATION, CallLog.Calls.NUMBER,
CallLog.Calls._ID },
CallLog.Calls.DATE + ">?" + " and "
+ CallLog.Calls.TYPE + "=?",
new String[] { timestamp, String.valueOf(CallLog.Calls.MISSED_TYPE) },
CallLog.Calls.DATE);
//Cursor managedCursor = this.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.DATE + ">= ?", new String[]{timestamp}, null);
//Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, where , null, CallLog.Calls.DATE+ " >= ?");
int number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = cursor.getColumnIndex(CallLog.Calls.TYPE);
int date = cursor.getColumnIndex(CallLog.Calls.DATE);
int duration = cursor.getColumnIndex(CallLog.Calls.DURATION);
{
while (cursor.moveToNext()) {
String LogphNumber = cursor.getString(number);
String callType = cursor.getString(type);
String callDate = cursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = cursor.getString(duration);
{
if(cursor.getCount()>1){
System.out.println("Total Logs::>>>>>>>>> "+cursor.getCount());
System.out.println("\nPhoneNo:----"+LogphNumber);
System.out.println("\nCallDate: "+callDayTime);
System.out.println("\nCallDuration in Seconds :"+callDuration);
System.out.println("\n-----------------------");
}else if(cursor.getCount()==0){
System.out.println("No Missed calls");
}
}
}cursor.close();
}
try this:
String timestamp = String.valueOf(getTodayTimestamp());
Cursor cursor = this.getContentResolver()
.query(CallLog.Calls.CONTENT_URI,
new String[] { CallLog.Calls.DATE, CallLog.Calls.TYPE,
CallLog.Calls.DURATION, CallLog.Calls.NUMBER,
CallLog.Calls._ID },
CallLog.Calls.DATE + ">?" + " and "
+ CallLog.Calls.TYPE + "=?",
new String[] { timestamp, String.valueOf(CallLog.Calls.MISSED_TYPE) },
CallLog.Calls.DATE);
Related
I have tried to get phone number also but i didn't no how to get using this code . This the code i am using please tell how to get number by same code for same id
// method to get name, contact id, and birthday
private Cursor getContactsBirthdays() {
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE
};
String where =
ContactsContract.Data.MIMETYPE + "= ? AND " +
ContactsContract.CommonDataKinds.Event.TYPE + "=" +
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[] {
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE
};
String sortOrder = null;
return managedQuery(uri, projection, where, selectionArgs, sortOrder);
}
// iterate through all Contact's Birthdays and print in log
Cursor cursor = getContactsBirthdays();
int bDayColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE);
while (cursor.moveToNext()) {
String bDay = cursor.getString(bDayColumn);
Log.d(TAG, "Birthday: " + bDay);
}
You need to first get a list of all contact-ids that has birthdays, then query for the phones of all those contacts, and then print the combined results.
Cursor cursor = getContactsBirthdays();
// get contact-ids for phones query
List<String> ids = new ArrayList<>();
while (cursor.moveToNext()) {
ids.add(cursor.getString(1));
}
String[] projection = new String[] { Phone.NUMBER, Phone.CONTACT_ID };
StringBuilder where = new StringBuilder(Data.MIMETYPE + " = " Phone.CONTENT_ITEM_TYPE + " AND " + Contacts.CONTACT_ID + " IN (");
for (String id : ids) {
where.append(id).append(",");
}
where.deleteCharAt(where.length() - 1);
where.append(")");
Cursor cur2 = getContentResolver().query(Data.CONTENT_URI, projection, where.toString(), null, null);
Map<Long, String> contactIdToPhone = new HashMap<>();
while (cur2.moveToNext()) {
contactIdToPhone.put(cur2.get(1), cur2.get(0));
}
cur2.close();
cursor.moveToPosition(-1);
while (cursor.moveToNext()) {
Long id = cursor.getLong(1);
Log.i(TAG, "Birthday: id=" + id + ", name=" + cursor.getString(0) + ", date=" + cursor.getString(2) + ", phone=" + contactIdToPhone.get(id));
}
cursor.close();
I have a TextView to which I'm putting my contacts which have birthdays:
private String loadContacts()
{
StringBuilder builder = new StringBuilder();
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null,
null, null);
if (cursor.getCount() > 0){
while (cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String columns[] = {
ContactsContract.CommonDataKinds.Event.START_DATE,
ContactsContract.CommonDataKinds.Event.TYPE,
ContactsContract.CommonDataKinds.Event.MIMETYPE,
};
String where = ContactsContract.CommonDataKinds.Event.TYPE + "=" +
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY +
" and " + ContactsContract.CommonDataKinds.Event.MIMETYPE +
" = '" + ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE +
"' and " + ContactsContract.Data.CONTACT_ID + " = " + id;
String sortOrder = ContactsContract.CommonDataKinds.Event.START_DATE + " ASC";
Cursor birthdayCur = cr.query(ContactsContract.Data.CONTENT_URI, columns, where,
null, sortOrder);
if (birthdayCur.getCount() > 0) {
while (birthdayCur.moveToNext()) {
String birthday = birthdayCur.getString(birthdayCur.
getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
builder.append("Contact: ").append(name).append(" ").append(birthday).append("\n\n");
}
}
birthdayCur.close();
}
}
cursor.close();
return builder.toString();
}
And then assign the contacts on the "onCreate" function
if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.READ_CONTACTS }, 1);
} else {
listContacts.setText(loadContacts());
}
This line of code:
String sortOrder = ContactsContract.CommonDataKinds.Event.START_DATE + " ASC";
Cursor birthdayCur = cr.query(ContactsContract.Data.CONTENT_URI, columns, where,
null, sortOrder);
doesn't seem to work no matter what I put there to order it (name, _ID etc.).
I am trying to sort by Birthdays but even if I try to sort by name it doesn't work. Why ?
Am I using wrong type of View(TextView) or is it the way I use StringBuilder to set the text or something else entirely?
My minSdkVersion is 19 and targetSdkVersion 26. I'm testing this App on my Nexus5(api 23 android 6.01) on debugging mode.
I appreciate any advice.
Well this is what happens when I hurried. It was actually beyond simple.
I just needed to put all my needed columns in the first Cursor and the second more important thing was to use "ContactsContract.Data.CONTENT_URI"
instead of "ContactsContract.Contacts.CONTENT_URI" because the "DATA.CONTENT_URI" actually has all of my needed columns and "Contacts.CONTENT_URI" doesn't.
As simple as that.
private String loadContacts()
{
StringBuilder builder = new StringBuilder();
ContentResolver cr = getContentResolver();
String columns[] = {
ContactsContract.CommonDataKinds.Event.START_DATE,
ContactsContract.Contacts.DISPLAY_NAME
};
String where = ContactsContract.CommonDataKinds.Event.TYPE + "=" +
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY +
" and " + ContactsContract.CommonDataKinds.Event.MIMETYPE +
" = '" + ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE + "'";
String sortOrder = ContactsContract.CommonDataKinds.Event.START_DATE + " ASC";
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
columns, where,
null, sortOrder);
if (cursor.getCount() > 0){
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String birthday = cursor.getString(cursor.
getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
builder.append("Contact: ").append(name).append(birthday).append(" ").append("\n\n");
}
}
cursor.close();
return builder.toString();
}
In my app i am getting the all call log by using this code.it return me all the call log in my android phone.
public class CallLogHelper {
public static Cursor getAllCallLogs(ContentResolver cr) {
// reading all data in descending order according to DATE
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
Uri callUri = Uri.parse("content://call_log/calls");
Cursor curCallLogs = cr.query(callUri, null, null, null, strOrder);
return curCallLogs;
}
public static void insertPlaceholderCall(ContentResolver contentResolver,
String name, String number) {
ContentValues values = new ContentValues();
values.put(CallLog.Calls.NUMBER, number);
values.put(CallLog.Calls.DATE, System.currentTimeMillis());
values.put(CallLog.Calls.DURATION, 0);
values.put(CallLog.Calls.TYPE, CallLog.Calls.OUTGOING_TYPE);
values.put(CallLog.Calls.NEW, 1);
values.put(CallLog.Calls.CACHED_NAME, name);
values.put(CallLog.Calls.CACHED_NUMBER_TYPE, 0);
values.put(CallLog.Calls.CACHED_NUMBER_LABEL, "");
Log.d("Call Log", "Inserting call log placeholder for " + number);
contentResolver.insert(CallLog.Calls.CONTENT_URI, values);
}
}
But my problem is this i want to get the call log from specific date, not all call log. I have no idea how to use the query to get the call log from specific date.
Help me Thanks
String[] strFields = {
android.provider.CallLog.Calls.NUMBER,
android.provider.CallLog.Calls.TYPE,
android.provider.CallLog.Calls.CACHED_NAME,
android.provider.CallLog.Calls.DATE,
android.provider.CallLog.Calls.DURATION,
};
// Defines a string to contain the selection clause
String mSelectionClause = android.provider.CallLog.Calls.DATE+ " >= ?";
// Initializes an array to contain selection arguments
String[] mSelectionArgs = { createDate(2013,1,1).toString() };
Cursor mCallCursor = currentContext.getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI,
strFields,
mSelectionClause,
mSelectionArgs,
null
);
return mCallCursor;
public static Long createDate(int year, int month, int day)
{
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
return calendar.getTimeInMillis();
}
StringBuffer sb = new StringBuffer();
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
Uri callUri = Uri.parse("content://call_log/calls");
Calendar calendar = Calendar.getInstance();
calendar.set(2014, Calendar.MAY, 25);
String fromDate = String.valueOf(calendar.getTimeInMillis());
calendar.set(2014, Calendar.MAY, 30);
String toDate = String.valueOf(calendar.getTimeInMillis());
String[] whereValue = {fromDate,toDate};
Cursor cur = cr.query(callUri, null, android.provider.CallLog.Calls.DATE+" BETWEEN ? AND ?", whereValue, strOrder);
//Cursor cur = cr.query(callUri, null, android.provider.CallLog.Calls.DATE+" >= ?", whereValue, strOrder);
// loop through cursor
while (cur.moveToNext()) {
String callNumber = cur.getString(cur
.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
String callName = cur
.getString(cur
.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
String callDate = cur.getString(cur
.getColumnIndex(android.provider.CallLog.Calls.DATE));
SimpleDateFormat formatter = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm");
String dateString = formatter.format(new Date(Long
.parseLong(callDate)));
String callType = cur.getString(cur
.getColumnIndex(android.provider.CallLog.Calls.TYPE));
String dir=null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
String isCallNew = cur.getString(cur
.getColumnIndex(android.provider.CallLog.Calls.NEW));
String duration = cur.getString(cur
.getColumnIndex(android.provider.CallLog.Calls.DURATION));
sb.append("\nPhone Number:--- " + callNumber + " \nName:--- "+ callName +" \nCall Type dir:--- " + dir + " \nCall Date:--- " + dateString + " \n duration in sec :--- " + duration);
sb.append("\n----------------------------------");
}
The call log that returned is between 25/05/2014 & 30/05/2014. The returned value is all stored in sb variable, you can set this value in a TextView for testing
I am able to fetch other information (Display name,organisation,phone no and email_id) of a contact, but not able to fetch birthday and anniversary of that contact.
Here is the code i am using for birthday. It does fetch the data, but gives me wrong data, i.e repeats the same data for all the contacts.
private String getBDate(String id) {
String bday = null;
ContentResolver cr = getContentResolver();
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE };
String where = ContactsContract.Data.MIMETYPE + "= ? AND "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[] { ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };
String sortOrder = null;
Cursor cur = cr.query(uri, projection, where, selectionArgs, sortOrder);
while (cur.moveToNext()) {
bday = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
Log.v("Birthday", bday);
}
cur.close();
return bday;
}
Same is the case with anniversary, here is the code for it. In some case anniversary is not added but it still shows the data from other contact.
private String getAnnv(String id) {
String annv = null;
ContentResolver cr = getContentResolver();
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE };
String where = ContactsContract.Data.MIMETYPE + "= ? AND "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY;
String[] selectionArgs = new String[] { ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };
// String sortOrder = null;
Cursor cur = cr.query(uri, projection, where, selectionArgs, null);
while (cur.moveToNext()) {
annv = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
Log.v("Anniversary", annv);
}
cur.close();
return annv;
}
you are not using String id perameter in where condition so please check again.
E,g private String getAnnv(String id) function has input for ID but that seems to be not used withing function so please put that ID in condition check and this should work.
e.g
ContactsContract.CommonDataKinds.Event.CONTACT_ID + "= " + ID
AND ContactsContract.Data.MIMETYPE + "= ? AND "
I want to list of events from my calendar based on specific date. I am using following
Cursor cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/events"), new String[]{ "_id", "title", "description", "dtstart", "dtend", "eventLocation" }, null, null, null);
This code works fine but it brings the total events from the calendar. But i need a events on specfic date like 2011-08-15. I tried following code
Cursor cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/events"), new String[]{ "_id", "title", "description", "dtstart", "dtend", "eventLocation" }, "dtstart", new String[]{sdf.format(calender.getTime())}, null);
cursor.moveToFirst();
But I am getting exception
Caused by: android.database.sqlite.SQLiteException: no such column:
Calendars.dtstart: , while compiling: SELECT _id, title, description,
dtstart, dtend, eventLocation FROM view_events WHERE (1) AND
(Calendars.dtstart)
Please give some advise to solve my problem
I have done this way:
String[] projection = new String[] { CalendarContract.Events.CALENDAR_ID, CalendarContract.Events.TITLE, CalendarContract.Events.DESCRIPTION, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, CalendarContract.Events.ALL_DAY, CalendarContract.Events.EVENT_LOCATION };
Calendar startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY,0);
startTime.set(Calendar.MINUTE,0);
startTime.set(Calendar.SECOND, 0);
Calendar endTime= Calendar.getInstance();
endTime.add(Calendar.DATE, 1);
String selection = "(( " + CalendarContract.Events.DTSTART + " >= " + startTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() + " ) AND ( deleted != 1 ))";
Cursor cursor = context.getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, null, null);
List<String> events = new ArrayList<>();
if (cursor!=null&&cursor.getCount()>0&&cursor.moveToFirst()) {
do {
events.add(cursor.getString(1));
} while ( cursor.moveToNext());
}
It will give today's event from Calendar content provider.
Note: Managed the deleted events from content provider.
Done
Refer to Calendar Provider Documentation this link
I am using this code and it is working..
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(Uri.parse("content://com.android.calendar/calendars"), (new String[] { Calendars._ID, Calendars.ACCOUNT_NAME, Calendars.CALENDAR_DISPLAY_NAME, }), null, null, null);
This methods is 100% worked for me however i tried several method . It will get all the event and store in a set . Then you can check the set whether a event is in the set or not . In this way you can check duplicate event
public Set<String> readCalendarEvent(Context context) {
Cursor cursor = context.getContentResolver()
.query(
Uri.parse("content://com.android.calendar/events"),
new String[] { "calendar_id", "title", "description",
"dtstart", "dtend", "eventLocation" }, null,
null, null);
cursor.moveToFirst();
// fetching calendars name
String CNames[] = new String[cursor.getCount()];
// fetching calendars id
calendars.clear();
Log.d("cnameslength",""+CNames.length);
if (CNames.length==0)
{
Toast.makeText(context,"No event exists in calendar",Toast.LENGTH_LONG).show();
}
for (int i = 0; i < CNames.length; i++) {
calendars.add(cursor.getString(1));
CNames[i] = cursor.getString(1);
cursor.moveToNext();
}
return calendars;
}
try this code i am in my application i am used this event for display event date wise;
ContentResolver contentResolver = mContext.getContentResolver();
final Cursor cursor = contentResolver.query(Uri.parse("content://com.android.calendar/calendars"),
(new String[] { "_id", "displayName", "selected" }), null, null, null);
HashSet<String> calendarIds = new HashSet<String>();
while (cursor.moveToNext()) {
final String _id = cursor.getString(0);
final String displayName = cursor.getString(1);
final Boolean selected = !cursor.getString(2).equals("0");
//System.out.println("Id: " + _id + " Display Name: " + displayName + " Selected: " + selected);
calendarIds.add(_id);
}
in this snipped you should get all calendar event in calendarIds...now check
for (String id : calendarIds) {
Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
long now = new Date().getTime();
ContentUris.appendId(builder, now - DateUtils.DAY_IN_MILLIS * 10000);
ContentUris.appendId(builder, now + DateUtils.DAY_IN_MILLIS * 10000);
Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + id,
null, "startDay ASC, startMinute ASC");
// For a full list of available columns see http://tinyurl.com/yfbg76w
while (eventCursor.moveToNext()) {
eventTitle = eventCursor.getString(0);
begin = new Date(eventCursor.getLong(1));
end = new Date(eventCursor.getLong(2));
allDay = !eventCursor.getString(3).equals("0");
eventDate = begin.getDate();
eventTime = begin.getHours();
eventMonth = begin.getMonth();
eventYear = begin.getYear();
event.add(eventTitle);
SimpleDateFormat sdfcur = new SimpleDateFormat("yyyy-MM-dd");
String beginString = sdfcur.format(begin);
Date begindt = null;
try {
begindt = sdfcur.parse(beginString);
stringEventDate = begindt.getDate();
stringEventMonth = begindt.getMonth();
stringEventYear = begindt.getYear();
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Date dt = null;
dt = sdfcur.parse(onClickDate);
int passedday=dt.getDate();
int passedmonth = dt.getMonth();
int passedyear = dt.getYear();