Android : I can't call a built in SMS method under the dialog box

on Tuesday, August 12, 2014


I am trying to have a built-in SMS after clicking on the YES dialog box. I retrieved the contacts from the address book.


public class ContactListActivity extends Activity implements OnItemClickListener {



private ListView listView;
private List<ContactBean> list = new ArrayList<ContactBean>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(this);

Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (phones.moveToNext()) {

String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

ContactBean objContact = new ContactBean();
objContact.setName(name);
objContact.setPhoneNo(phoneNumber);
list.add(objContact);

}
phones.close();

ContanctAdapter objAdapter = new ContanctAdapter(
ContactListActivity.this, R.layout.alluser_row, list);
listView.setAdapter(objAdapter);

if (null != list && list.size() != 0) {
Collections.sort(list, new Comparator<ContactBean>() {

@Override
public int compare(ContactBean lhs, ContactBean rhs) {
return lhs.getName().compareTo(rhs.getName());
}
});
AlertDialog alert = new AlertDialog.Builder(
ContactListActivity.this).create();
alert.setTitle("");

alert.setMessage(list.size() + " Contact Found!!!");

alert.setButton("OK", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();

} else {
showToast("No Contact Found!!!");
}
}

private void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

@Override
public void onItemClick(AdapterView<?> listview, View v, int position,
long id) {
ContactBean bean = (ContactBean) listview.getItemAtPosition(position);
showCallDialog(bean.getName(), bean.getPhoneNo());
}

private void showCallDialog(String name, final String phoneNo) {
AlertDialog alert = new AlertDialog.Builder(ContactListActivity.this)
.create();
alert.setTitle("Find?");

alert.setMessage("Are you sure want to find " + name + " ?");

alert.setButton("No", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.setButton2("Yes", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
sendSMS(phoneNo, "Where are you?");
}
});
alert.show();
}

private void sendSMS(String phoneNumber, String message){
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}


}


0 comments:

Post a Comment