How to access MISSED_CALL Log in Android - android

I am developing an app in which i want to access MISSED_CALL log. Using below code....
private Cursor getItemsToSync() {
G = "Log method accessing";
ContentResolver r = getContentResolver();
String selections = String.format("%s > ?", CallLog.Calls.DATE,CallLog.Calls.MISSED_TYPE);
String[] selectionArgs = new String[] { String.valueOf(getMaxSyncedDate())};
String sortOrder = SmsConsts.DATE + " LIMIT " + PrefStore.getMaxItemsPerSync(this);
N = CallLog.Calls.CACHED_NAME;
return r.query(Uri.parse("content://call_log/calls"), null,selections,selectionArgs, sortOrder);}
its provide All Call Log. Please suggest me how to get only MISSED_CALL Call log. Thanks in advance

String[] strFields = {android.provider.CallLog.Calls.CACHED_NAME, android.provider.CallLog.Calls.NUMBER,android.provider.CallLog.Calls.DATE, android.provider.CallLog.Calls.TYPE
};
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
Cursor mCallCursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,strFields, null, null, strOrder);
if (mCallCursor.moveToFirst()) {
do {
boolean missed = mCallCursor.getInt(mCallCursor.getColumnIndex(CallLog.Calls.TYPE)) == CallLog.Calls.MISSED_TYPE;
if (missed) {
String name = mCallCursor.getString(mCallCursor
.getColumnIndex(CallLog.Calls.CACHED_NAME));
String number = mCallCursor.getString(mCallCursor
.getColumnIndex(CallLog.Calls.NUMBER));
Log.d("PhoneLog", "You have a missed call from " + name + " on " + number // + " at " + time); }
} while (mCallCursor.moveToNext());
}

Related

Retrieving contacts

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();

How to compare a particular field to a string in SQLite

The Cons.getpersonIdForRegisteredUser() method returns a string.
My query has two parts: one is to check the person id and the other is data enabled
#Override
public Loader<Cursor> onCreateLoader(final int i, final Bundle bundle) {
String sortOrder = ContactProviderContract.ContactDatabaseEntry.COLUMN_NAME_FIRST_NAME
+ " COLLATE NOCASE ASC";
String selection = ContactProviderContract.
ContactDatabaseEntry.COLUMN_NAME_PERSON_ID + " != " + Cons.getPersonIdForRegisteredUser(getActivity().getApplicationContext())
+ " AND "
+ ContactProviderContract.ContactDatabaseEntry.COLUMN_NAME_DATA_ENABLED + " = ?";
String[] selectionArgs = { "1" };
return new CursorLoader(getActivity().getApplicationContext(),
ContactProviderContract.CONTACTS_URI,
ContactProviderContract.ContactDatabaseEntry.MIN_DETAILS_COLUMNS,
selection,
selectionArgs,
sortOrder);
}
The error is that the string retrieved from the method Cons.getpersonid...() is considered as a column and not as a string to compare.
If I understand the error correctly, then you should place that Cons.getPersonId... into the selectionArgs.
String selection = ContactProviderContract.
ContactDatabaseEntry.COLUMN_NAME_PERSON_ID + " != ?" +
+ " AND "
+ ContactProviderContract.ContactDatabaseEntry.COLUMN_NAME_DATA_ENABLED + " = ?";
String[] selectionArgs = { "1" , Cons.getPersonIdForRegisteredUser(getActivity().getApplicationContext()) };

Getting specific contact information

I'm trying to get the given name, family name etc of a specific contact on android but can't seem to be able to. I've been banging my head on this for hours, basically if (nameCur.moveNext()) is always false! This code is originally by #perborin (How to get the first name and last name from Android contacts?). Please help!
P.S. I've added <uses-permission android:name="android.permission.READ_CONTACTS"/> in the AndroidManifest, so that is not the problem.
// A contact ID is fetched from ContactList
Uri resultUri = data.getData();
Cursor cont = getContentResolver().query(resultUri, null, null, null, null);
if (!cont.moveToNext()) {
Toast.makeText(this, "Cursor contains no data", Toast.LENGTH_LONG).show();
return;
}
int columnIndexForId = cont.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cont.getString(columnIndexForId);
// Fetch contact name with a specific ID
String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = " + contactId;
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
Cursor nameCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
Toast.makeText(this, "Name: " + given + " Family: " + family + " Displayname: " + display, Toast.LENGTH_LONG).show();
}
nameCur.close();
cont.close();
Solved by referring to user3717188 answer at Get first and last name of a contact rather than single display name?.
Cursor phone_cursor = cr.query(ContactsContract.CommonDataKinds.
Phone.CONTENT_URI, null, null, null, null);
while (phone_cursor.moveToNext()) {
try {
int id = Integer.parseInt(phone_cursor.getString(phone_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)));
Cursor name_cursor = cr.query(ContactsContract.Data.CONTENT_URI,null,
ContactsContract.Data.CONTACT_ID + " = " + id, null, null);
String name = phone_cursor.getString(phone_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String first_name ="";
String last_name = "";
while (name_cursor.moveToNext()) {
if(name_cursor.getString(name_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME))!=null){
first_name = name_cursor.getString(name_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
last_name = name_cursor.getString(name_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
}}
name_cursor.close();
String phoneNumber = phone_cursor.getString(phone_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER));
} catch (Exception e) {
}
}
phone_cursor.close();

Query to the ContactsContract database comparing empty fields with empty strings - Android

I'm creating methods to edit a contact's field.
Before editing I'm making sure that those fields do exist and correspond to the company I want to edit, so I make a select and look for if(cursor.moveToFirst()) but it is not going inside the if for some reason I can not understand. I just know that the problem is on my empty Strings as if I remove them the code works fine.
I've downloaded the phones contact database so I know exactly what is in the contacts database. That is why I'm hardcoding the variables.
Here is my code:
public Boolean editRawContactOrganization(String rawContactId, String oldCompany, String oldType, String oldLabel, String oldTitle,
String oldDepartment, String oldJobDescription, String oldSymbol, String oldPhoneticName, String oldLocation,
String newCompany, String newType, String newLabel, String newTitle, String newDepartment, String newJobDescription,
String newSymbol, String newPhoneticName, String newOfficeLocation)
{
final ContentValues cv = new ContentValues();
final String filter = Organization.RAW_CONTACT_ID + "=? AND "
+ Organization.COMPANY + "=? AND "
+ Organization.TYPE_WORK + "=? AND "
+ Organization.LABEL + "=? AND "
+ Organization.TITLE + "=? AND "
+ Organization.DEPARTMENT + "=? AND "
+ Organization.JOB_DESCRIPTION + "=? AND "
+ Organization.SYMBOL + "=? AND "
+ Organization.PHONETIC_NAME + "=? AND "
+ Organization.OFFICE_LOCATION + "=? AND "
+ Data.MIMETYPE + "=?";
oldCompany = "Tone Inc.";
oldType = "2";
oldLabel = "";
oldTitle = "The Big Boss";
oldDepartment = "";
oldJobDescription = "";
oldSymbol = "";
oldPhoneticName = "";
oldLocation = "";
final String[] selectionArgs = new String[] {rawContactId, oldCompany, oldType, oldLabel, oldTitle, oldDepartment, oldJobDescription,
oldSymbol, oldPhoneticName, oldLocation, Organization.CONTENT_ITEM_TYPE};
final String[] projection = new String[] { Organization._ID };
final Cursor cursor = contentResolver.query(Data.CONTENT_URI,
projection,
filter,
selectionArgs,
null);
if(cursor.moveToFirst())
{
Log.d(TAG, "SUCCESS: Organization found! ID: " + cursor.getString(0));
cursor.close();
}
else
{
Log.d(TAG, "ERROR: Organization not found...");
cursor.close();
return false;
}
return false;
}
As you can see, these vars are hard coded with the corresponding values I see on the contacts database I just downloaded from the phone.
oldCompany = "Tone Inc.";
oldType = "2";
oldLabel = "";
oldTitle = "The Big Boss";
oldDepartment = "";
oldJobDescription = "";
oldSymbol = "";
oldPhoneticName = "";
oldLocation = "";
But I always get the ERROR: Organization not found... output on logcat
Can anyone point me to where is the error?
Here is a working example, if I uncoment the commented code I get the not found output (the same problem as above). Same pattern as above, the commented column has no value inside. Even when I'm sending and empty string why doesn't is match empty with empty?
public Boolean editRawContactWebsite(String rawContactId, String oldWebsite, String oldType, String oldLabel,
String newWebsite, String newType, String newLabel)
{
final ContentValues cv = new ContentValues();
final String filter = Website.RAW_CONTACT_ID + "=? AND "
+ Website.URL + "=? AND "
+ Website.TYPE + "=? AND "
//+ Website.LABEL + "=? AND "
+ Data.MIMETYPE + "=?";
oldWebsite = "www.sitr.com";
oldType = "7";
oldLabel = "";
final String[] selectionArgs = new String[] {rawContactId, oldWebsite, oldType, /*oldLabel,*/ Website.CONTENT_ITEM_TYPE};
final String[] projection = new String[] { Website._ID };
final Cursor cursor = contentResolver.query(Data.CONTENT_URI,
projection,
filter,
selectionArgs,
null);
if(cursor.moveToFirst())
{
Log.d(TAG, "SUCCESS: website found! ID: " + cursor.getString(0));
cursor.close();
}
else
{
Log.d(TAG, "ERROR: website not found...");
cursor.close();
return false;
}
return false;
}
I fixed this problem by passing an ID corresponding to the line I want to edit in the contacts database.

Selecting/Joining tables from contacts table but get no ouput - Android

I'm trying to go over all contacts using the contacts, raw_contacts and data tables.
From reading online I believe that Contacts._id is connected with RawContacts.contact_id and Data.raw_contact_id is connected with RowContacts._id. Am I right?
Believing in this I built this method:
public void testingContactsDatabase(String contactId)
{
final String[] projection = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME,
Data.RAW_CONTACT_ID,
Data.MIMETYPE,
StructuredName.DISPLAY_NAME,
StructuredName.FAMILY_NAME,
StructuredName.GIVEN_NAME,
StructuredName.MIDDLE_NAME,
StructuredName.PREFIX, // Common prefixes in English names are "Mr", "Ms", "Dr" etc.
StructuredName.SUFFIX // Common suffixes in English names are "Sr", "Jr", "III" etc.
};
final String selection = Contacts._ID + " = " + contactId + " AND "
+ Contacts._ID + " = " + RawContacts.CONTACT_ID + " AND "
+ Data.RAW_CONTACT_ID + " = " + RawContacts._ID;
final Cursor curStructuredName = context.getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
projection,
selection, null,
//new String[] {contactId, ContactsContract.RawContacts.CONTACT_ID, RawContacts._ID},
null
);
if(curStructuredName.moveToFirst())
{
Log.d("XYZ", "Found something");
}
}
But it never finds anything. I'm not sure if I'm using the correct URI, I've tried with different URIs and either got exceptions or same ouput.
Can someone point me out where I'm doing something wrong?
Thanks
I've solved my problem. Here is my code working now:
// Get the structured name fields
public ArrayList<String> getStructuredName(String contactId)
{
ArrayList<String> structuredList = new ArrayList<String>();
final String[] projection = new String[] {
StructuredName.DISPLAY_NAME,
StructuredName.FAMILY_NAME,
StructuredName.GIVEN_NAME,
StructuredName.MIDDLE_NAME,
StructuredName.PREFIX, // Common prefixes in English names are "Mr", "Ms", "Dr" etc.
StructuredName.SUFFIX // Common suffixes in English names are "Sr", "Jr", "III" etc.
};
final String filter = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?";
final String parameters[] = {StructuredName.CONTENT_ITEM_TYPE, contactId};
Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
projection, filter, parameters, null);
if(cursor.moveToFirst())
{
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
{
final String displayName = cursor.getString(cursor.getColumnIndex(StructuredName.DISPLAY_NAME));
final String familyName = cursor.getString(cursor.getColumnIndex(StructuredName.FAMILY_NAME));
final String givenName = cursor.getString(cursor.getColumnIndex(StructuredName.GIVEN_NAME));
final String middleName = cursor.getString(cursor.getColumnIndex(StructuredName.MIDDLE_NAME));
final String prefix = cursor.getString(cursor.getColumnIndex(StructuredName.PREFIX));
final String suffix = cursor.getString(cursor.getColumnIndex(StructuredName.SUFFIX));
structuredList.add(displayName);
structuredList.add(familyName);
structuredList.add(givenName);
structuredList.add(middleName);
structuredList.add(prefix);
structuredList.add(suffix);
Log.d(tag, "/////////////////////////////////////////////////////");
Log.d(tag, "displayName: " + displayName );
Log.d(tag, "familyName: " + familyName );
Log.d(tag, "givenName: " + givenName );
Log.d(tag, "middleName: " + middleName );
Log.d(tag, "prefix: " + prefix );
Log.d(tag, "suffix: " + suffix );
Log.d(tag, "/////////////////////////////////////////////////////");
}
}
cursor.close();
return structuredList;
}

Categories

Resources