我创建了一个按钮,当点击该按钮向我们显示手机通讯录列表时,我希望从列表中选择一个联系人并向他发送消息。
我的应用程序允许您通过"联系人"手动输入我想要链接联系人所选联系人的号码来发送消息。按钮和发送短信
有什么想法吗?
编辑:此处为联系人代码
@Override public void onActivityResult(int reqCode, int resultCode, Intent data){ super.onActivityResult(reqCode, resultCode, data);
switch(reqCode)
{
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK)
{
Uri contactData = data.getData();
Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, 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();
String cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// Toast.makeText(getApplicationContext(), cNumber, Toast.LENGTH_SHORT).show();
}
}
}
}
}
ħ
contact = (Button) findViewById(R.id.BoutonContact);
contact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
我发送短信方式:
protected void envoiSMS(){ Log.i(" Envoyer SMS","");
String phoneNum = txtPhoneNum.getText().toString();
String message = txtMessage.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNum, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS envoyé", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Echec à l'envoi du SMS veuillez réessayer", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
答案 0 :(得分:1)
试试这个:
@Override
public void onItemClick(AdapterView<?>adapter,View v, int position){
ItemClicked item = adapter.getItem(position);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + item.getPhoneNumber()));
intent.putExtra("sms_body", item.getMessageBody());
startActivity(intent);
}
编辑:
@Override public void onActivityResult(int reqCode,int resultCode,Intent data){super.onActivityResult(reqCode,resultCode,data);
switch(reqCode)
{
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK)
{
Uri contactData = data.getData();
Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, 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();
String cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// Toast.makeText(getApplicationContext(), cNumber, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + cNumber));
startActivity(intent);
}
}
}
}
}