How to call Android contacts list?

前端 未结 13 1993
傲寒
傲寒 2020-11-22 02:50

I\'m making an Android app, and need to call the phone\'s contact list. I need to call the contacts list function, pick a contact, then return to my app with the contact\'s

相关标签:
13条回答
  • 2020-11-22 03:20
            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 Number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    
            }
    
    0 讨论(0)
  • 2020-11-22 03:28
    -> Add a permission to read contacts data to your application manifest.
    
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    
    -> Use Intent.Action_Pick in your Activity like below
    
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
    
    -> Then Override the onActivityResult()
    
     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // check whether the result is ok
            if (resultCode == RESULT_OK) {
                // Check for the request code, we might be usign multiple startActivityForReslut
                switch (requestCode) {
                case RESULT_PICK_CONTACT:
                   Cursor cursor = null;
            try {
                String phoneNo = null ;
                String name = null;           
                Uri uri = data.getData();            
                cursor = getContentResolver().query(uri, null, null, null, null);
                cursor.moveToFirst();           
                int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                phoneNo = cursor.getString(phoneIndex); 
    
                textView2.setText(phoneNo);
            } catch (Exception e) {
                e.printStackTrace();
            }
                    break;
                }
            } else {
                Log.e("MainActivity", "Failed to pick contact");
            }
        }
    
    This will work check it out
    
    0 讨论(0)
  • 2020-11-22 03:29

    Looking around for an API Level 5 solution using ContactsContract API you could slightly modify the code above with the following:

      Intent intent = new Intent(Intent.ACTION_PICK);
      intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
      startActivityForResult(intent, PICK_CONTACT);
    

    And then in onActivityResult use the column name:

      ContactsContract.Contacts.DISPLAY_NAME
    
    0 讨论(0)
  • 2020-11-22 03:29

    hi i have a code to save the contact in your database by shared preference here is my code

    public class Main22Activity extends AppCompatActivity {
        EditText nameInput,phoneInput;
        TextView LargeText;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main22);
            nameInput = (EditText) findViewById(R.id.nameInput);
            phoneInput = (EditText) findViewById(R.id.phoneInput);
    
            LargeText = (TextView) findViewById(R.id.textView2);
        }
    
        public void saveInfo (View view){
            SharedPreferences sharedPref = getSharedPreferences("nameInfo" , Context.MODE_PRIVATE);
            SharedPreferences.Editor editor= sharedPref.edit();
            editor.putString("name", nameInput.getText().toString());
            editor.putString("phone", phoneInput.getText().toString());
            editor.apply();
            Toast.makeText(this, "Saved", Toast.LENGTH_LONG).show();
        }
    
        public void displayData(View view){
            SharedPreferences sharedPref = getSharedPreferences("nameInfo" , Context.MODE_PRIVATE);
            String name = sharedPref.getString("name", "");
            String ph = sharedPref.getString ("phone","");
            LargeText.setText(name + " " + ph);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:29

    I use the code provided by @Colin MacKenzie - III. Thanks a lot!

    For someone who are looking for a replacement of 'deprecated' managedQuery:

    1st, assuming using v4 support lib:

    import android.support.v4.app.LoaderManager;
    import android.support.v4.content.CursorLoader;
    import android.support.v4.content.Loader;
    

    2nd:

    your_(activity)_class implements LoaderManager.LoaderCallbacks<Cursor>
    

    3rd,

    // temporarily store the 'data.getData()' from onActivityResult
    private Uri tmp_url;
    

    4th, override callbacks:

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // create the loader here!
        CursorLoader cursorLoader = new CursorLoader(this, tmp_url, null, null, null, null);
        return cursorLoader;
    }
    
    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        getContactInfo(cursor); // here it is!
    }
    
    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
    }
    

    5th:

    public void initLoader(Uri data){
        // will be used in onCreateLoader callback
        this.tmp_url = data;
        // 'this' is an Activity instance, implementing those callbacks
        this.getSupportLoaderManager().initLoader(0, null, this);
    }
    

    6th, the code above, except that I change the signature param from Intent to Cursor:

    protected void getContactInfo(Cursor cursor)
    {
    
       // Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);      
       while (cursor.moveToNext()) 
       {
            // same above ...
       } 
    

    7th, call initLoader:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (PICK_CONTACT == requestCode) {
            this.initLoader(data.getData(), this);
        }
    }
    

    8th, don't forget this piece of code

        Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
        this.act.startActivityForResult(intentContact, PICK_CONTACT);
    

    References:

    Android Fundamentals: Properly Loading Data

    Initializing a Loader in an Activity

    0 讨论(0)
  • 2020-11-22 03:33

    To my surprise you do not need users-permission CONTACT_READ to read the names and some basic information (Is the contact starred, what was the last calling time). However you do need permission to read the details of the contact like phone number.

    0 讨论(0)
提交回复
热议问题