I am new to android development. I want to monitor the changes occurred in the call log.My code is fetching all the call history until the last recent call log.when I run my app it again fetches all the logs including the newest one but I want only the latest changes. Please guide me how can I achieve this with contentOberser ? Please help. this is my code
private void conatctDetails()
{
StringBuffer sb = new StringBuffer();
Cursor c1 = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME);
sb.append("Contact details:");
try
{
while(c1.moveToNext())
{
String id = c1.getString(c1.getColumnIndex(ContactsContract.Contacts._ID));
String name =c1.getString(c1.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String timesContacted =c1.getString(c1.getColumnIndex(ContactsContract.Contacts.TIMES_CONTACTED));
String lastTimeContacted =c1.getString(c1.getColumnIndex(ContactsContract.Contacts.LAST_TIME_CONTACTED));
String number="";
// String number =c1.getString(c1.getColumnIndex(CommonDataKinds.Phone.NUMBER));
if(id==null||name==null)
continue;
Cursor cur = getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, null, CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
if(cur==null)
continue;
number = "";
while(cur.moveToNext())
{
number = cur.getString(cur.getColumnIndex(CommonDataKinds.Phone.NUMBER));
sb.append("\n Contact Number:--"+number);
}
sb.append("\n Contact Name:--"+name+"\n Contact ID:--"+id+"\n Last Time Contacted:--"+lastTimeContacted+"\n Total Time Contacted:--"+timesContacted);
sb.append("\n----------------------------");
}
//c1.close();
contact.setText(sb);
}
catch(Exception e)
{
}
finally
{
c1.close();
}
}
If there is an Intent
that is fired on an incoming call you could get your app to respond by using a BroadcastReceiver
and create an IntentFilter
in your manifest.
Or just query the provider when your app launches...
ACTION_PHONE_STATE_CHANGED
is a TelephonyManager
intent you can listen for and check if it is TelephonyManager.CALL_STATE_RINGING
.