如何在Android设备中获取最近的联系人列表

时间:2019-01-23 09:12:12

标签: android android-contacts contactscontract

我需要获取最近24小时内最近使用且最常用的联系人列表。

我进行了很多搜索,但没有找到任何方法。我还知道Google已撤销URI以获取经常使用的联系人:https://developer.android.com/reference/android/provider/ContactsContract.Contacts#CONTENT_FREQUENT_URI

但是没有给出该URI的替代品吗? 请让我知道实现的方法:

  1. 获取最近24小时内最近联系的联系人列表。
  2. 获取最常用的3个联系人。

2 个答案:

答案 0 :(得分: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;
    }
}


}

答案 1 :(得分:0)

不推荐使用Contacts表中的CONTENT_FREQUENT_URI以及TIMES_CONTACTEDLAST_TIME_CONTACTED字段的全部目的是防止访问您所用的信息正在寻找。

Google现在将此信息视为敏感的用户信息,并且不允许应用程序继续获取该信息。

但是,根据我的经验,似乎我所知道的或我们的用户使用的所有设备仍然允许访问已弃用的API,因此,如果您需要在明年或大多数时间内可以满足大多数用户的需求,因此,您仍然可以使用它。

代码应类似于:

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