【问题标题】:How to get the recent contact list in android device如何在android设备中获取最近的联系人列表
【发布时间】:2019-01-23 09:12:12
【问题描述】:

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

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

但是没有给出这个 URI 的替代品是什么? 请告诉我实现的方法:

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

【问题讨论】:

  • 您能否接受答案或指出还需要什么?谢谢。

标签: android android-contacts contactscontract


【解决方案1】:

弃用联系人表中的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

【讨论】:

    【解决方案2】:
    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;
        }
    }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 2020-10-16
      • 1970-01-01
      相关资源
      最近更新 更多