How to call Android contacts list?

前端 未结 13 1994
傲寒
傲寒 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:47

    The complete code is given below

        package com.testingContect;
    
        import android.app.Activity;
        import android.content.Intent;
        import android.database.Cursor;
        import android.net.Uri;
        import android.os.Bundle;
        import android.provider.ContactsContract;
        import android.provider.Contacts.People;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.Toast;
    
        public class testingContect extends Activity implements OnClickListener{
        /** Called when the activity is first created. */
    
        EditText ed;
        Button bt;
        int PICK_CONTACT;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            bt=(Button)findViewById(R.id.button1);
            ed =(EditText)findViewById(R.id.editText1);
            ed.setOnClickListener(this);
            bt.setOnClickListener(this);
    
    
        }
    
        @Override
        public void onClick(View v) {
    
            switch(v.getId())
            {
            case R.id.button1:
    
                break;
            case R.id.editText1:
                 Intent intent = new Intent(Intent.ACTION_PICK);
                  intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
                  startActivityForResult(intent, PICK_CONTACT);
    
    
                break;
    
            }
    
        }
        public void onActivityResult(int requestCode, int resultCode, Intent intent) 
        {
    
          if (requestCode == PICK_CONTACT)
          {         
              Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);
              cursor.moveToNext();
              String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
               String  name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
    
              Toast.makeText(this, "Contect LIST  =  "+name, Toast.LENGTH_LONG).show(); 
          }
        }//onActivityResult
    }//class ends
    
    0 讨论(0)
提交回复
热议问题