1

I need to fetch out the list of contacts that are recently used in last 24 hours and most frequently used.

I have searched a lot but did not find any way to do that. I also come to know that google has revoked the URIs to get frequently used contacts : https://developer.android.com/reference/android/provider/ContactsContract.Contacts#CONTENT_FREQUENT_URI

But what is the substitute of this URI is not given? Please let me know the ways to achieve:

  1. Fetch list of contacts contacted recently in last 24 hours.
  2. Fetch the top 3 most frequently used contacts.
marmor
  • 27,641
  • 11
  • 107
  • 150
Manmohan Soni
  • 6,472
  • 2
  • 23
  • 29

2 Answers2

1

The whole point of deprecating the CONTENT_FREQUENT_URI as well as the TIMES_CONTACTED and LAST_TIME_CONTACTED fields in the Contacts table are to prevent apps from accessing the information you're looking for.

Google now considers this info to be sensitive user info, and will not allow apps to obtain that going forward.

However, from my experience, it seems that all devices I know of or are used by our users still allow access to the deprecated API, so if you need something that will be ok for most of your users within the next year or so, you can still use it.

Code should be something like:

String[] projection = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Contacts.LAST_TIME_CONTACTED };

Cursor lastContacted = getContentResolver().query(Contacts.CONTENT_URI, projection, Contacts.LAST_TIME_CONTACTED + " < " + lastDayTimestamp, null, Contacts.LAST_TIME_CONTACTED + " DESC");
DatabaseUtils.dumpCursor(lastContacted);

Cursor mostContacted = getContentResolver().query(Contacts.CONTENT_URI, projection, null, null, Contacts.TIMES_CONTACTED + " DESC");
DatabaseUtils.dumpCursor(mostContacted); // might want to limit this to 3
marmor
  • 27,641
  • 11
  • 107
  • 150
0
public class MainActivity extends AppCompatActivity {

ListView listView ;
ArrayList<String> StoreContacts ;
ArrayAdapter<String> arrayAdapter ;
Cursor cursor ;
String name, phonenumber ;
public  static final int RequestPermissionCode  = 1 ;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    listView = (ListView)findViewById(R.id.listview1);

    button = (Button)findViewById(R.id.button1);

    StoreContacts = new ArrayList<String>();

    EnableRuntimePermission();

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            GetContactsIntoArrayList();

            arrayAdapter = new ArrayAdapter<String>(
                    MainActivity.this,
                    R.layout.contact_items_listview,
                    R.id.textView, StoreContacts
            );

            listView.setAdapter(arrayAdapter);


        }
    });

}

public void GetContactsIntoArrayList(){

    cursor = 
getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
null,null, null, null);

    while (cursor.moveToNext()) {

        name = 
  cursor.getString(cursor.getColumnIndex 
(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAM 
   E));

        phonenumber = 

 cursor.getString(cursor.getColumnIndex 
(ContactsContract.CommonDataKinds.Phone.NUMBER));

        StoreContacts.add(name + " "  + ":" + " " + phonenumber);
    }

    cursor.close();

}

public void EnableRuntimePermission(){

    if (ActivityCompat.shouldShowRequestPermissionRationale(
            MainActivity.this,
            Manifest.permission.READ_CONTACTS))
    {

        Toast.makeText(MainActivity.this,"CONTACTS permission allows us to Access 
CONTACTS app", Toast.LENGTH_LONG).show();

    } else {

        ActivityCompat.requestPermissions(MainActivity.this,new String[]{
                Manifest.permission.READ_CONTACTS}, RequestPermissionCode);

    }
}

@Override
public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {

    switch (RC) {

        case RequestPermissionCode:

            if (PResult.length > 0 && PResult[0] == 
PackageManager.PERMISSION_GRANTED) {

                Toast.makeText(MainActivity.this,"Permission Granted, Now your 
application can access CONTACTS.", Toast.LENGTH_LONG).show();

            } else {

                Toast.makeText(MainActivity.this,"Permission Canceled, Now your 
application cannot access CONTACTS.", Toast.LENGTH_LONG).show();

            }
            break;
    }
}


}