我想要做的是有一个 EditText,在那里我可以输入一些名称(这样就会出现一个过滤列表,其中的名称与我迄今为止输入的内容相对应)。最后我选择了一个联系人。EditText 应显示我选择的姓名,但会向与所选联系人对应的号码发送消息(短信)。
这是我的代码,不完整:当然我在 AndroidManifest 文件中也有一些设置..
公共类 SendSMSActivity 扩展活动 {
Button buttonSend;
EditText textPhoneNo;
EditText textSMS;
String sms ="";
ListAdapter lAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sendsms);
buttonSend = (Button) findViewById(R.id.buttonSend);
textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
textPhoneNo.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
String srchName = textPhoneNo.getText().toString();
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null,
ContactsContract.Contacts.HAS_PHONE_NUMBER
+ " = 1 AND "
+ ContactsContract.Contacts.DISPLAY_NAME
+ " like " + "'" + srchName + "%'",
null,
"UPPER(" + ContactsContract.Contacts.DISPLAY_NAME
+ ") ASC");
startManagingCursor(cursor);
Load(cursor);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
textSMS = (EditText) findViewById(R.id.editTextSMS);
sms = MainActivityClass.tempSms.toString();
Log.d("SendSMSActivity", " sms text = " + sms);
textSMS.setText(sms);
textSMS.setVisibility(EditText.VISIBLE);
buttonSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String phoneNo = textPhoneNo.getText().toString();
//String sms = textSMS.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
}