从移动电话的目录中检索联系人

时间:2016-09-16 15:45:21

标签: java android

此代码可以在移动设备上打开内置联系人目录。但是,当我选择联系人时,应用程序报告“已完成”,但未检索联系人号码 - 用户无法发送消息。

如何检索联系电话? List View不是解决方案:我只需返回一个数字,而不是额外信息。

这是我的代码:

public EditText no;
public EditText msg;
public Button cancel;
public Button snd;
public String Number,name,Message;
public int run=0;
public static final int PICK_CONTACT=1;



public void callContacts(View v)
{
    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(intent,PICK_CONTACT);
}
@Override
public void onActivityResult(int reqCode,int resultCode,Intent data)
{
    super.onActivityResult(reqCode,resultCode,data);
    if(reqCode==PICK_CONTACT)
    {
        if(resultCode== ActionBarActivity.RESULT_OK)
        {
            Uri contactData = data.getData();
            Cursor c = getContentResolver().query(contactData,null,null,null,null);
            if(c.moveToFirst())
            {
                name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.CONTENT_URI.toString()));
                Toast.makeText(this,"done",Toast.LENGTH_SHORT).show();
            }
        }
    }
}

这是onCreate方法

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_second);

    no = (EditText)findViewById(R.id.num);
    msg = (EditText)findViewById(R.id.sms);
    cancel = (Button)findViewById(R.id.cancel);
    snd = (Button)findViewById(R.id.snd);
    no.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            msg.setText("");
            no.setText("");
            callContacts(null);
        }
    });
    snd.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Number = no.toString();
            Message = msg.getText().toString();
            SmsManager manager = SmsManager.getDefault();
            ArrayList<String>long_sms = manager.divideMessage(Message);
            manager.sendMultipartTextMessage(Number,null,long_sms,null,null);
            Toast.makeText(getApplicationContext(),"Sent Successfully",Toast.LENGTH_SHORT).show();
        }
    });
}

1 个答案:

答案 0 :(得分:0)

以下是从联系人列表中选择一个电话号码的代码。我发现这个代码最简单。如果有任何问题,请告诉我。

使用电话数据类型的选择意图启动活动:

findViewById(R.id.choose_contact_button).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
            Intent pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(pickContact, PICK_CONTACT_REQUEST);
      }
 });

现在设置onAcitivityResult();

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent){
        switch (requestCode){
            case PICK_CONTACT_REQUEST:
                if (resultCode == RESULT_OK){
                    Uri contactUri = intent.getData();
                    Cursor nameCursor = getContentResolver().query(contactUri, null, null, null, null);
                    if (nameCursor.moveToFirst()){
                        String name = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String number = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        ((EditText)findViewById(R.id.person_name)).setText(name);
                        ((EditText)findViewById(R.id.enter_mobile)).setText(number);
                        nameCursor.close();
                    }
                }
                break;
        }
    }