如何在android设备中获取最近的联系人列表
我需要拿出最近24小时内最常用的联系人列表 我搜索了很多,但没有找到任何方法。我还了解到,谷歌已撤销URI以获取常用联系人: 但是没有给出这个URI的替代物是什么? 请让我知道实现以下目标的方法:如何在android设备中获取最近的联系人列表,android,android-contacts,contactscontract,Android,Android Contacts,Contactscontract,我需要拿出最近24小时内最常用的联系人列表 我搜索了很多,但没有找到任何方法。我还了解到,谷歌已撤销URI以获取常用联系人: 但是没有给出这个URI的替代物是什么? 请让我知道实现以下目标的方法: 获取最近24小时内联系的联系人列表 获取前3个最常用的联系人 public类MainActivity扩展了AppCompatActivity{ 列表视图列表视图; ArrayList存储联系人; ArrayAdapter ArrayAdapter; 光标; 字符串名称、电话号码; 公共静态最终int
public类MainActivity扩展了AppCompatActivity{
列表视图列表视图;
ArrayList存储联系人;
ArrayAdapter ArrayAdapter;
光标;
字符串名称、电话号码;
公共静态最终int RequestPermissionCode=1;
按钮;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(listView)findViewById(R.id.listview1);
按钮=(按钮)findViewById(R.id.button1);
StoreContacts=newarraylist();
EnableRuntimePermission();
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
GetContactsIntoArrayList();
arrayAdapter=新的arrayAdapter(
这个,,
R.layout.contact\u items\u listview,
R.id.textView,StoreContacts
);
setAdapter(arrayAdapter);
}
});
}
public void GetContactsInToaryList()中的联系人{
光标=
getContentResolver().query(ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,
空,空,空,空);
while(cursor.moveToNext()){
姓名=
cursor.getString(cursor.getColumnIndex
(contacts contract.commondatatypes.Phone.DISPLAY\u NAM)
E) );
电话号码=
cursor.getString(cursor.getColumnIndex
(ContactsContract.CommonDataTypes.Phone.NUMBER));
StoreContacts.add(姓名+“”+:“+“”+电话号码);
}
cursor.close();
}
public void EnableRuntimePermission(){
如果(ActivityCompat.shouldShowRequestPermissionRegulation)(
这个,,
清单。权限。读取(联系人))
{
makeText(MainActivity.this)允许我们访问
联系人应用程序“,Toast.LENGTH_LONG).show();
}否则{
ActivityCompat.requestPermissions(MainActivity.this,新字符串[]{
Manifest.permission.READ_CONTACTS},RequestPermissionCode);
}
}
@凌驾
public void onRequestPermissionsResult(int-RC,字符串per[],int[]PResult){
开关(RC){
案例请求许可代码:
如果(预设长度>0&&预设长度[0]==
PackageManager.权限(已授予){
makeText(MainActivity.this,“已授予权限,现在是您的
应用程序可以访问联系人。“,Toast.LENGTH_LONG).show();
}否则{
Toast.makeText(MainActivity.this,“权限已取消,现在您的
应用程序无法访问联系人。“,Toast.LENGTH_LONG).show();
}
打破
}
}
}
在联系人表中弃用内容\u频繁\u URI
以及联系次数
和上次联系时间
字段的全部目的是防止应用程序访问您正在查找的信息
谷歌现在认为这些信息是敏感的用户信息,今后不会允许应用程序获取这些信息
然而,根据我的经验,我所知道的或用户使用的所有设备似乎仍然允许访问不推荐使用的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
String[]projection=新字符串[]{Contacts.\u ID,Contacts.DISPLAY\u NAME,Contacts.LAST\u TIME\u CONTACTED};
游标lastContacted=getContentResolver().query(Contacts.CONTENT\u URI,投影,Contacts.LAST\u TIME\u CONTACTED+“<”+lastDayTimestamp,null,Contacts.LAST\u TIME\u CONTACTED+“DESC”);
DatabaseUtils.dumpCursor(lastContacted);
游标mostContacted=getContentResolver().query(Contacts.CONTENT_URI,projection,null,null,Contacts.TIMES_CONTACTED+“DESC”);
DatabaseUtils.dumpCursor(最有关联);//可能要将此限制为3
请您接受回答或说明还需要什么?非常感谢。
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