如何调出联系人以发送文本

Posted

技术标签:

【中文标题】如何调出联系人以发送文本【英文标题】:How to bring contacts up to send a text 【发布时间】:2013-04-11 14:44:47 【问题描述】:

我有一个应用程序,它定义了一个词,然后通过 SMS 发送定义。问题是用户必须输入电话号码。我在想也许应用程序应该显示联系人列表,以便用户可以选择一个联系人,然后用户可以将消息发送给该联系人。这是我的 SMSActivity 代码:

public class SMSActivity extends Activity 


String APPTAG = "SMSTransmit";

//Private static strings:
private final static String SENT = "SMS_SENT";
private final static String DELIVERED = "SMS_DELIVERED";

private Button btnSend;
private Button btnExit; 
private Button btnClear;
private EditText etPhoneNumber;
private EditText etUserMessage;

//Private BroadcastReceiver member variables:
private SMSDispatchReceiver sendReceiver = null;
private SMSReceiptReceiver receiptReceiver = null;


@Override
public void onCreate(Bundle savedInstanceState) 

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sms);

    Bundle extras = getIntent().getExtras();
    String definiedWord = null; 
    if (extras != null) 
    
        definiedWord = extras.getString("DEFINITION"); 
    
    else 
    
        definiedWord = "None"; 
    

    Log.d("recievedAGAIN", definiedWord);
    Log.v(APPTAG, "MainActivity: onCreate() called");

    //Create a new broadcast receiver for sending the SMS
    sendReceiver = new SMSDispatchReceiver();

    //Create a new broadcast receiver for receipt of the send SMS 
    receiptReceiver = new SMSReceiptReceiver();

    //Register the new receivers (as new IntentFiler() objects):
    registerReceiver(sendReceiver, new IntentFilter(SENT));
    registerReceiver(receiptReceiver, new IntentFilter(DELIVERED));

    //Get the view objects:
    btnSend = (Button) findViewById(R.id.btnSend);
    btnClear = (Button) findViewById(R.id.btnClear);
    btnExit = (Button) findViewById(R.id.btnExit);
    etPhoneNumber = (EditText) findViewById(R.id.etNumber);
    etUserMessage = (EditText) findViewById(R.id.etMessage);

    //Disable the soft keyboard (this is only in general):
    InputMethodManager inMethodMgr =                                                                (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    inMethodMgr.hideSoftInputFromInputMethod(etPhoneNumber.getWindowToken(), 0);
    inMethodMgr.hideSoftInputFromInputMethod(etUserMessage.getWindowToken(), 0);

    //Set the hint for the EditText boxes:
    etPhoneNumber.setHint("Enter phone number (Default = 5556)");
    etUserMessage.setHint(definiedWord);

    //Create an click event listener for the Send button (OnClickListener):
    btnSend.setOnClickListener(new View.OnClickListener() 
    

        @Override
        public void onClick(View v) 
        

            String strMessage = null;
            String strNumber = null;

            //Get the phone number from the EditText box:
            strNumber = etPhoneNumber.getText().toString();

            //Check the phone number:
            if (strNumber.length() <= 0) 
            

                //No phone number, then get the default (5556):
                strNumber = SMSProperties.getPhoneNumber();
            


            //Get the message from the EditText box:
            strMessage = etUserMessage.getText().toString();

            //Check the message contains some content:
            if (strMessage.length() > 0) 
            

                //Sent the SMS to the phone number 
                sendSMS(strNumber, strMessage);  

             else 
            

                //Warn the user and reset the focus / hint:
                Toast.makeText(getBaseContext(), "No message text! Please      enter some text for the SMS!!", Toast.LENGTH_SHORT).show();
                etUserMessage.setHint("Enter message text here . . . ");
                etUserMessage.requestFocus();

            

        
    );



    //Create an click event listener for the Clear button (OnClickListener):
    btnClear.setOnClickListener(new View.OnClickListener() 

        @Override
        public void onClick(View v) 

            //Clear the appropriate EditText box:
            if (etUserMessage.isFocused()) 

                //Clear the text in the user message EditText box:
                etUserMessage.setText("");

                //Set the hint in the user message EditText box:
                etUserMessage.setHint("Enter message text here . . . ");

             else if (etPhoneNumber.isFocused()) 

                //Clear the text in the phone number EditText box:
                etPhoneNumber.setText("");

                //Set the hint in the phone number EditText box:
                etPhoneNumber.setHint("Enter phone number (Default = 5556)");
            
        
    );



    //Create an click event listener for the Exit button (OnClickListener):
    btnExit.setOnClickListener(new View.OnClickListener()
    

        @Override
        public void onClick(View v) 
        

            //Finish the activity:
            //NOTE: As this is the main activity it will cause onDestroy() to be called!
            finish();
        
    );





//Method to send an SMS message to another device:
private void sendSMS(String strNum, String strMsg)
                   
    //TODO: Create a pending intent for the SMSDistatchReceiver (SENT):
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent("SENT"), 0);

    //TODO: Create a pending intent for the SMSReceiptReceiver (DELIVERED):
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent("DELIVERED"), 0);
    //Get a default SmsManager object:
    SmsManager smsMgr = SmsManager.getDefault();

    //TODO: Send the SMS using the SmsManager:  
    smsMgr.sendTextMessage(strNum, null, strMsg, sentPI, deliveredPI);  

【问题讨论】:

【参考方案1】:

考虑联系人是一个弹出联系人列表的按钮

contact.setOnClickListener(new View.OnClickListener()              
            @Override
            public void onClick(View g) 
                Intent q = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
                startActivityForResult(q, 1001);  

            
        );

并处理结果(由 1001 引用)使用:

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

        if (resultCode == Activity.RESULT_OK)   
            // getting the URI from result for further working
            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();
                      //this string will hold the contact number
                      String cNumber = phones.getString(phones.getColumnIndex("data1"));
                      //this string will hold the contact name
                      String cName = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

                  

             
         

【讨论】:

以上是关于如何调出联系人以发送文本的主要内容,如果未能解决你的问题,请参考以下文章

如何收集特定令牌以向特定设备发送推送通知

使用带有联系人电话号码的自动完成文本视图

如何使用本地 xampp 和 php 联系人表单从我的 mac 发送邮件

如何从android中的联系人中搜索号码?

创建自动完成文本视图以显示联系人

加载联系人以检索电子邮件地址并发送电子邮件