向Android中的选定联系人发送消息

时间:2016-09-06 08:36:33

标签: android message android-contacts

我想构建一个应用程序,在应用程序激活时通过帮助向选定的联系人发送消息。当点击激活切换键时,我创建了一个选择联系人但无法添加消息功能的应用程序

 textView1 = (TextView) findViewById(R.id.contact_number1);
 textView2 = (TextView) findViewById(R.id.contact_number2); 

public void pickAContactNumber(View view) 
{ 
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
 startActivityForResult(intent, PICK_CONTACT1);
 } 

1 个答案:

答案 0 :(得分:0)

由于您有获取联系的呼叫活动,您将获得onActivityResult方法的联系,并从那里您可以实现您的逻辑,如下所示,

  @Override
  public void onActivityResult(int reqCode, int resultCode, Intent data) {
     super.onActivityResult(reqCode, resultCode, data);

     switch (reqCode) {
         case (PICK_CONTACT1) :
             if (resultCode == Activity.RESULT_OK) {

                Uri contactData = data.getData();
                Cursor c =  managedQuery(contactData, null, null, null, null);
                if (c.moveToFirst()) {

                    String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                    String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                    if (hasPhone.equalsIgnoreCase("1")) {
                        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, 
                   null, null);
                            phones.moveToFirst();
                            cNumber = phones.getString(phones.getColumnIndex("data1"));
                            System.out.println("number is:"+cNumber);
                    sendMessage(cNumber);

                    }
                    //String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                }
           }
           break;
      }
  }

  public void sendMessage(String number)
  {
  }