I am trying to get all contacts in the favourites list of the Android contacts. Currently, I can get all the group ids including the favourite group ID. But it seems that there is no contacts that have the group ID as the favourite group ID.
I'm trying to get All groups id and contacts in each group. After printing two list, I found that the group id of favorite is not in the contact list
ArrayList<String> favGroupId=new ArrayList<String>();
final String[] GROUP_PROJECTION = new String[] {
ContactsContract.Groups._ID, ContactsContract.Groups.TITLE };
Cursor cursor = getContentResolver().query(
ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, null,
null, ContactsContract.Groups.TITLE);
while (cursor.moveToNext()) {
String id = cursor.getString(cursor
.getColumnIndex(ContactsContract.Groups._ID));
Log.v("Test",id);
String gTitle = (cursor.getString(cursor
.getColumnIndex(ContactsContract.Groups.TITLE)));
Log.v("Test",gTitle);
if (gTitle.contains("Favorite_")) {
gTitle = "Favorites";
favGroupId.add(id);
}
}
cursor.close();
You can use the STARRED field in the ContactsContract.Contact class. If you change your query to:
Cursor cursor = this.managedQuery(
ContactsContract.Contacts.CONTENT_URI, projection, "starred=?",
new String[] {"1"}, null);
this should return a list of all contacts that appear in the Favorites tab in the default Contacts app on Android.
Complete answer, including intentUriString for opening the contact with an Intent:
Map getFavoriteContacts(){
Map contactMap = new HashMap();
Uri queryUri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED};
String selection =ContactsContract.Contacts.STARRED + "='1'";
Cursor cursor = managedQuery(queryUri, projection, selection, null, null);
while (cursor.moveToNext()) {
String contactID = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
intent.setData(uri);
String intentUriString = intent.toUri(0);
String title = (cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
contactMap.put(title,intentUriString);
}
cursor.close();
return contactMap;
}
Ty this with Kotlin:
import android.content.Context
import android.provider.ContactsContract
import android.content.Intent
import android.net.Uri
fun getFavoriteContacts(context: Context): Map<*, *> {
lateinit var contactMap : HashMap<String, String>
val queryUri = ContactsContract.Contacts.CONTENT_URI.buildUpon()
.appendQueryParameter(ContactsContract.Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true")
.build()
val projection = arrayOf(
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED
)
val selection = ContactsContract.Contacts.STARRED + "='1'"
val cursor = context.getContentResolver().query(queryUri,
projection, selection, null, null)
while (cursor.moveToNext()) {
val contactID = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID))
val intent = Intent(Intent.ACTION_VIEW)
val uri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_URI, contactID.toString())
intent.data = uri
val intentUriString = intent.toUri(0)
val title = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
contactMap[title] = intentUriString
}
cursor.close()
return contactMap
}
Here is the complete method to get contact details with favorite in Java
ContentResolver contentResolver = context.getContentResolver();
if (contentResolver == null)
return;
String[] fieldListProjection = {
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts.PHOTO_URI
,ContactsContract.Contacts.STARRED
};
String sort = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " ASC";
Cursor phones = contentResolver
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI
, fieldListProjection, null, null, sort);
HashSet<String> normalizedNumbersAlreadyFound = new HashSet<>();
if (phones != null && phones.getCount() > 0) {
while (phones.moveToNext()) {
String normalizedNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if (Integer.parseInt(phones.getString(phones.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
if (normalizedNumbersAlreadyFound.add(normalizedNumber)) {
int id = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int fav = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STARRED));
boolean isFav;
isFav= fav == 1;
String uri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if(uri!=null){
contactList.add(new FavContact(id,isFav,name,phoneNumber,uri));
}
else{
contactList.add(new FavContact(id,isFav,name,phoneNumber));
}
}
}
}
phones.close();
}
Related
I have function to load all contact number in android, and now i want to filter all contact number with prefix "0878","0877","0817" so contact number with that prefix will be loaded, and here my current code :
private void loadContact(){
ContentResolver cr = applicationContext.getContentResolver();
String [] PROJECTION = ContactsQuery.PROJECTION;
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PROJECTION,
null,
null,
null
);
while (cursor.moveToNext()) {
//String name = cursor.getString(ContactsQuery.DISPLAY_NAME);
String lookUpKey = cursor.getString(ContactsQuery.LOOKUP_KEY);
loadContactDetail(lookUpKey);
}
cursor.close();
}
private void loadContactDetail(String lookUpKey){
ContentResolver cr = applicationContext.getContentResolver();
String [] COLS = {ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, COLS,
ContactsContract.Data.LOOKUP_KEY + " = ?",
new String[]{lookUpKey},
null
);
while (cursor.moveToNext()) {
String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String formatted = Util.formatMSISDN(phone);
if(formatted != null)
contactsSet.add(formatted);
}
cursor.close();
}
Try this.
while (cursor.moveToNext()) {
String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String formatted = Util.formatMSISDN(phone);
if(formatted != null){
if(formatted.startsWith("0878") || formatted.startsWith("0877") || formatted.startsWith("0817"))
contactsSet.add(formatted);
}
}
cursor.close();
Check out the sourceString.contains(CharSequence) method.
I had many query and it was taking lot of time to fetch contact data.
So need to convert all query in into one.
I got all other info like display name, mobile number, label, email in one query
but finding prob only with getting pics
Help me to Convert below photo query into single query and get uri of pics :
ContentResolver KntRslverVar = getContentResolver();
Cursor ContctKsrVar = KntRslverVar.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER},
null, null, null);
while (ContctKsrVar.moveToNext())
{
String ContctUidVar = ContctKsrVar.getString(ContctKsrVar.getColumnIndex(ContactsContract.Contacts._ID));
String ContctNamVar = ContctKsrVar.getString(ContctKsrVar.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String ContctMobVar = ContctKsrVar.getString(ContctKsrVar.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Cursor ContctPflPicKsrVar = KntRslverVar.query(
ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[]{ContctUidVar, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE},
null);
if (ContctPflPicKsrVar != null && ContctPflPicKsrVar.getCount() > 0)
{
while(ContctPflPicKsrVar.moveToNext())
{
Uri ContctPflPicUriVar = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(ContctUidVar));
PflPicUriVar = Uri.withAppendedPath(ContctPflPicUriVar, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
ContctPflPicKsrVar.close();
}
}
Try 1 :
Cursor ContctKsrVar = KntRslverVar.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE},
null, null, null);
Try 2 :
Cursor ContctKsrVar = KntRslverVar.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI},
null, null, null);
Also need help on getting uri from fetched query :
Uri KctPflPicUriVar = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(KctUidVar));
PflPicUriVar = Uri.withAppendedPath(KctPflPicUriVar, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
|*| Optimised : Get Display Name and Number with label and Photo Thumbnail in one query : Gets result query in less than 4 sec.
ContentResolver contntRslverVar = getContentResolver();
Cursor contctKsrVar = contntRslverVar.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.LABEL,
ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI},
null, null, null);
while (contctKsrVar.moveToNext())
{
String contctUidVar = contctKsrVar.getString(contctKsrVar.getColumnIndex(ContactsContract.Contacts._ID));
String contctNamVar = contctKsrVar.getString(contctKsrVar.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String contctMobVar = contctKsrVar.getString(contctKsrVar.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int MobLblTypVar = contctKsrVar.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int MobLblTipVar = contctKsrVar.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL);
MobLblTypVar = contctKsrVar.getInt(MobLblTypVar);
String contctMobLblVar;
if(MobLblTypVar == ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM)
{
contctMobLblVar = contctKsrVar.getString(MobLblTipVar);
}
else
{
CharSequence MobLblSrgVar = ContactsContract.CommonDataKinds.Phone.getTypeLabel(getResources(), MobLblTypVar, "Mobile");
contctMobLblVar = MobLblSrgVar.toString();
}
String PflPicSrgVar = contctKsrVar.getString(contctKsrVar.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI));
if(PflPicSrgVar != null) Uri PflPicUriVar = Uri.parse(PflPicSrgVar);
}
I know to retrieve the names of contacts that's code below. But what should I change in this code to also have the numbers of phone associated with the contact list?
var uri = ContactsContract.Contacts.ContentUri;
string[] projection = { ContactsContract.Contacts.InterfaceConsts.Id,
ContactsContract.Contacts.InterfaceConsts.DisplayName };
var cursor = ManagedQuery(uri, projection, null, null, null);
var contactList = new List<string>();
if (cursor.MoveToFirst())
{
do
{
String phoneNumber = cursor.GetString(cursor.GetColumnIndex(ContactsContract.CommonDataKinds.Phone.Number));
contactList.Add(cursor.GetString(
cursor.GetColumnIndex(projection[1])));
} while (cursor.MoveToNext());
}
I came across your question while I recently wrote a similar code. The only thing you need in order to retrieve phone numbers, is to:
Make another query to Uri
ContactsContract.CommonDataKinds.Phone.ContentUri;
Set projection to include ContactsContract.CommonDataKinds.Phone.Number; and
Select based on id, i.e. selection parameter should include "_id = " +
contactId
Your query should look like this:
string[] projection = { ontactsContract.CommonDataKinds.Phone.Number };
string selection = "_id = " + contactId;
var cursor = ContentResolver.Query(ContactsContract.CommonDataKinds.Phone.ContentUri, projection, selection, null, null);
This query will bring you phone numbers for a contact id but in code below, due to performance issues, I just made one call for all phone numbers and then assign each contract their numbers. Hope it helps.
private List<Contact> GetContactList()
{
List<Contact> contacts = new List<Contact>();
string[] projection = {
ContactsContract.Contacts.InterfaceConsts.Id,
ContactsContract.Contacts.InterfaceConsts.DisplayName,
ContactsContract.Contacts.InterfaceConsts.PhotoUri
};
var uri = ContactsContract.Contacts.ContentUri;
ICursor cursor = ContentResolver.Query(uri, projection, null, null, null);
if (cursor.MoveToFirst())
{
do
{
string id = cursor.GetString(cursor.GetColumnIndex(projection[0]));
string name = cursor.GetString(cursor.GetColumnIndex(projection[1]));
string photoUri = cursor.GetString(cursor.GetColumnIndex(projection[2]));
contacts.Add(new Contact() { Id = long.Parse(id), DisplayName = name, PhotoUri = photoUri });
} while (cursor.MoveToNext());
GetContactPhoneNumber(contacts);
}
return contacts;
}
private async void GetContactPhoneNumber(List<Contact> list)
{
string[] projection =
{
ContactsContract.CommonDataKinds.Phone.Number,
ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId
};
var cursor = ContentResolver.Query(ContactsContract.CommonDataKinds.Phone.ContentUri, projection, null, null, null);
if (cursor.Count > 0)
{
await Task.Factory.StartNew(() =>
{
do
{
try
{
string id = cursor.GetString(cursor.GetColumnIndex(projection[1]));
string phoneNumber = cursor.GetString(cursor.GetColumnIndex(projection[0]));
Contact contact = list.Where(c => c.Id == long.Parse(id)).FirstOrDefault();
contact.PhoneNumber = phoneNumber;
}
catch
{
}
} while (cursor.MoveToNext());
cursor.Close();
});
}
}
i want to select unique contacts from android only that contacts which have phone numbers. i am using this code
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, ContactsContract.Contacts.DISPLAY_NAME);
// Find the ListView resource.
mainListView = (ListView) findViewById(R.id.mainListView);
// When item is tapped, toggle checked properties of CheckBox and
// Planet.
mainListView
.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View item,
int position, long id)
{
ContactsList planet = listAdapter.getItem(position);
planet.toggleChecked();
PlanetViewHolder viewHolder = (PlanetViewHolder) item
.getTag();
viewHolder.getCheckBox().setChecked(planet.isChecked());
}
});
// Create and populate planets.
planets = (ContactsList[]) getLastNonConfigurationInstance();
// planets = new Planet[10];
// planets.Add("asdf");
ArrayList<ContactsList> planetList = new ArrayList<ContactsList>();
String phoneNumber = null;
String phoneType = null;
count = cur.getCount();
contacts = new ContactsList[count];
if (planets == null)
{
if (cur.getCount() > 0)
{
planets = new ContactsList[cur.getCount()];
int i = 0;
//
while (cur.moveToNext())
{
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
// Query phone here. Covered next
Cursor pCur = cr
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[]
{ id }, null);
// WHILE WE HAVE CURSOR GET THE PHONE NUMERS
while (pCur.moveToNext())
{
// Do something with phones
phoneNumber = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
phoneType = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
Log.i("Pratik", name + "'s PHONE :" + phoneNumber);
Log.i("Pratik", "PHONE TYPE :" + phoneType);
}
pCur.close();
}
planets = new ContactsList[]
{ new ContactsList(name, phoneNumber) };
contacts[i] = planets[0];
planetList.addAll(Arrays.asList(planets));
i++;
}
}
this code retrieve all the contacts and put the into a list. but i want unique contacts and only that which have phone no. how can i do this?? is there any method to pass some argument in query to select unique contacts only???
I think you mean you got duplicate record for some contacts. So you must add condition for your query. The essential part is contacts must be in visible group and have phone number.
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ ("1") + "'";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
cur = context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, projection, selection
+ " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
+ "=1", null, sortOrder);// this query only return contacts which had phone number and not duplicated
Update 20/05/2020
suspend fun fetchContacts(): ArrayList<FriendItem> {
val list = ArrayList<FriendItem>()
val uri: Uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
val selection = ContactsContract.Contacts.HAS_PHONE_NUMBER
val cursor: Cursor? = context.contentResolver.query(
uri,
arrayOf(
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.Contacts._ID
),
selection,
null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"
)
cursor?.let {
val nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)
val phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
while (cursor.moveToNext()) {
val info = FriendItem(
friendName = cursor.getString(nameIndex),
friendPhoneNumber = cursor.getString(phoneIndex)
)
list.add(info)
}
cursor.close()
}
return list
}
This is working for me to get contact with phone number. Here we are querying Data table, and using CONTACT_ID contact provider documentation
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
final String ORDER_BY = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " ASC";
final String[] PROJECTION = {
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
return new CursorLoader(
context,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PROJECTION,
null,
null,
ORDER_BY
);
}
easy way to get phonenumbers and contact names
// set as global
Set<string> phonenumbersList = new HashSet<string>();
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//contact has name number and phonenumber does not exists in list
if ( phoneNumber != null && name != null && !phonenumbersList.contains(phoneNumber)){
planets = new ContactsList[]{ new ContactsList(name, phoneNumber) };
phonenumbersList.add(phoneNumber);
planetList.addAll(Arrays.asList(planets));
planetList.Add(phoneNumber, name);
}
}
phones.close();
I need to call recent call list and favourite call list on the click of respective buttons and need the data in my own list layout.I am new to android and having lot of trouble with this.can anyone please help me..thanks in advance..
With some extra, useful code:
getFavoriteContacts:
Map getFavoriteContacts(){
Map contactMap = new HashMap();
Uri queryUri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED};
String selection =ContactsContract.Contacts.STARRED + "='1'";
Cursor cursor = getContentResolver().query(queryUri, projection, selection,null,null);
while (cursor.moveToNext()) {
String contactID = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
intent.setData(uri);
String intentUriString = intent.toUri(0);
String title = (cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
contactMap.put(title,intentUriString);
}
cursor.close();
return contactMap;
}
getRecentContacts:
Map getRecentContacts(){
Map contactMap = new HashMap();
Uri queryUri = android.provider.CallLog.Calls.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
CallLog.Calls._ID,
CallLog.Calls.NUMBER,
CallLog.Calls.CACHED_NAME,
CallLog.Calls.DATE};
String sortOrder = String.format("%s limit 500 ", CallLog.Calls.DATE + " DESC");
Cursor cursor = getContentResolver().query(queryUri, projection, null,null,sortOrder);
while (cursor.moveToNext()) {
String phoneNumber = cursor.getString(cursor
.getColumnIndex(CallLog.Calls.NUMBER));
String title = (cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)));
if(phoneNumber==null||title==null)continue;
String uri = "tel:" + phoneNumber ;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
String intentUriString = intent.toUri(0);
contactMap.put(title,intentUriString);
}
cursor.close();
return contactMap;
}
For getting recent calls list,you can use CallLog in android. Here is a good tutorial.This is also helpful.
You can use it for all outgoing calls like this :
Cursor cursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null, android.provider.CallLog.Calls.TYPE+"="+android.provider.CallLog.Calls.OUTGOING_TYPE, null,null);
For all types of calls,use it like:
Cursor cursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null, null, null,null);
You can use the following Java code to get the recent calls list:
Cursor phones = getActivity().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null,null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex(CallLog.Calls.NUMBER));
if (phoneNumber.length() > 6) {
String name = phones.getString(phones.getColumnIndex(CallLog.Calls.CACHED_NAME));
numbers.add(phoneNumber);
}
}