Sending SMS in Android Application - android

I'm trying to build a project that shows how you can send SMS via Android Application.
I have built the project (in eclipse) using the SmsManager object, according to the code here: http://www.tutorialspoint.com/android/android_sending_sms.htm
the project is running good, but the SMS isn't sent to me.
I guess that there is something else I need to add in order that the SMS actually be sent - so what am i missing?
Or maybe I'm not inserting the number correctly? how should I write it? I tried a few ways...
Any help will be deeply appriciated!
Thank you.

Here is the code for selecting the contact from the contact list on button click:
Button x;
String phn_no, msg,c;
x.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case (1):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(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();
phn_no = phones.getString(phones
.getColumnIndex("data1"));
// Toast.makeText(getApplicationContext(), phn_no,
// Toast.LENGTH_LONG).show();
contact_num.setText(phn_no);
// String name =
// c.getString(c.getColumnIndex(StructuredPostal.DISPLAY_NAME));
// Toast.makeText(this, "contact info : "+
// phn_no+"\n"+name, Toast.LENGTH_LONG).show();
}
}
}
}
}
This is the code for sending SMS:
void send_sms(final String phn_no, final String msg) {
final SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phn_no, null, msg, null, null);
}

Related

Pass phone number as destination of my message

Am trying to pass the phone number I picked from contact list of my device. I have edittext to write my message and then by one click I want to pick a number from the list and send at same time. Access phone number is confusing to me rather than access the name itself.( I don't need the name, i want to send my message to him/her as they have phone number.) I'm having java.lang.IllegalArgumentException: Invalid destinationAddress for sms.sendTextMessage**
public void Send(View view){
String myMsg = myMessage.getText().toString();
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(no,null,myMsg,null,null);//"8044842795
}
String no = "";
#Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(reqCode, resultCode, data);
if(reqCode == PICK_CONTACT) {
if(resultCode == ActionBarActivity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cur = getContentResolver().query(contactData, null, null, null, null);
ContentResolver contect_resolver = getContentResolver();
if (cur.moveToFirst()) {
String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
if (phoneCur.moveToFirst()) {
no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
}
}
}
You need to send the SMS in onActivityResult. Until that is called, you don't know who was chosen so you can't get their number.

how can i get phone number of selected contact

c.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Intent i = new Intent(ContactsContract.Contacts.CONTENT_TYPE);
Intent i = new Intent(Intent.ACTION_PICK);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(i, CONTACT_PICKER_RESULT); }
});
}
public void onActivityResult(int requestCode ,int resultCode ,Intent data) {
if ((requestCode == CONTACT_PICKER_RESULT) && (resultCode == RESULT_OK)) {
}
}
This code contains the intent opens the contacts but I need if select contact take a contact phone number then set it to string
Check this solution: How to call Android contacts list?
Its pretty much what you need to do but instead name you should search the number with the cursor.
PS: Next time search better, there is alot of people asking it.
You can try this.
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
if(reqCode == this.pickContact){
if (resultCode == Activity.RESULT_OK) {
Log.d("ContactsH", "ResOK");
Uri contactData = data.getData();
Cursor contact = getContentResolver().query(contactData, null, null, null, null);
if (contact.moveToFirst()) {
String name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact's name.
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + name + "'", null, null);
if (cursor.moveToFirst()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
switch (type) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
// do something with the Home number here...
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
// do something with the Mobile number here...
Log.d("ContactsH", number);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
// do something with the Work number here...
break;
}
}
phones.close();
}
cursor.close();
}
}
}else{
Log.d("ContactsH", "Canceled");
}
}
That works for me.

setting contact number to edittext in android

I am trying to get the phone number by the code below but setting the number to the EditText field seems not to work.
The code in onActivityResult() is not giving me the contact name from the selected contacts.
EditText number;
public void chooseContact(View v) {
contact = (ImageView) findViewById(R.id.quickContact);
contact.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, PICK_CONTACT);
}
});
// Toast.makeText(getApplicationContext(), "hi contact is selected!!",
// Toast.LENGTH_SHORT).show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
ContentResolver cr = getContentResolver();
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null,
null, null);
if (c.moveToFirst()) {
id = c.getString(c
.getColumnIndex(ContactsContract.Contacts._ID));
name = c.getString(c
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(c.getString(c
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = ?", new String[] { id },
null);
while (pCur.moveToNext()) {
cnumber = pCur.getString(pCur
.getColumnIndex(Phone.NUMBER));
// Toast.makeText(getApplicationContext(), cnumber,
// Toast.LENGTH_SHORT).show();
number.setText(cnumber);
}
}
}
}
}
try
number.setText(cnumber+"");
or
number.setText(String.valueOf(cnumber));
Updates:
Change your logic
int i = 0;
while (pCur.moveToNext()) {
cnumber = pCur.getString(pCur
.getColumnIndex(Phone.NUMBER));
// Toast.makeText(getApplicationContext(), cnumber,
// Toast.LENGTH_SHORT).show();
if(i == 0){
number.setText(cnumber);
break;
}
}
here. Because this loop will set the last number only in your number edittext.
You didn't find your EditText in the code, number = findViewById(R.id.edittext), try finding and executing it.
It's also showing the error. So, to solve -
1.
id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
//Instead of this place String keyword at starting.
2.
cnumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//Place String keyword at starting.
3.
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//Place String keyword at starting.

geting force close when selecting google contacts

hi I am working on an app that sends SMS to selected contact.
to select a contact I am using this code
public void selectRecipient(View V)
{
Intent pickContactIntent=new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(pickContactIntent,1);
}
My onActivityResult function is following
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK)
{
if(requestCode==1)
{
count++;
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
c.moveToFirst();
name= c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String id = c.getString(c.getColumnIndex(Contacts._ID));
Cursor cur =getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, null, CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
cur.moveToNext();
String phoneNumber = cur.getString(cur.getColumnIndex(CommonDataKinds.Phone.NUMBER));
}
}
}
when I select any google contact in contact list, I get force close.
If I select a phone contact that has anumber then it's working fine.
What should I do to rectify this?
you will need to Check Current contact has at least one phone number Using ContactsContract.Contacts.HAS_PHONE_NUMBER and Close Cursor using Cursor.close(); After Getting Phone Number . So Change your Code as:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case (1) :
{
if (resultCode == Activity.RESULT_OK)
{
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
c.moveToFirst();
String phoneNumber=this.getContactPhone(c);
}
break;
}
}
}
private String getContactPhone(Cursor cursor)
{
int phoneColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
int phoneNum = cursor.getInt(phoneColumn);
String phoneResult="";
//System.out.print(phoneNum);
if (phoneNum > 0)
{
int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cursor.getString(idColumn);
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + contactId,
null, null);
//int phoneCount = phones.getCount();
//allPhoneNum = new ArrayList<String>(phoneCount);
if (phones.moveToFirst())
{
for (;!phones.isAfterLast();phones.moveToNext())
{
int index = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int typeindex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int phone_type = phones.getInt(typeindex);
String phoneNumber = phones.getString(index);
switch(phone_type)
{
case 2:
phoneResult=phoneNumber;
break;
}
//allPhoneNum.add(phoneNumber);
}
if (!phones.isClosed())
{
phones.close();
}
}
}
return phoneResult;
}
and Make Sure you have following permission in AndroidManifest.xml .
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

Getting contact number using content provider in android

I followed this tutorial and got the basics of Content Providers : http://www.vogella.de/articles/AndroidSQLite/article.html
But wanted to know how can i get the contact number that is stored against display name. tried with "ContactsContract.Contacts.CONTENT_VCARD_TYPE". But got an error.
Please let me know if there is any solution.
Thanks
Sneha
This is a good tutorial about Getting contact number using content provider in android
http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/
and
http://www.app-solut.com/blog/2012/06/retrieval-of-contacts-with-contact-contract/
and can pick contact number like this
add button click event like
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, PICK_CONTACT);
}
});
//outside button click
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
getContactInfo(data);
}
}
}
// onActivityResult
private void getContactInfo(Intent data) {
// TODO Auto-generated method stub
ContentResolver cr = getContentResolver();
Cursor cursor = managedQuery(data.getData(), null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
Name = cursor
.getString(cursor
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{contactId}, null);
emailCur.close();
if (hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false";
if (Boolean.parseBoolean(hasPhone)) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
while (phones.moveToNext()) {
phoneNo = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
pname.setText(Name);
//
phno.setText(phoneNo);
Toast.makeText(this, Name + " " + phoneNo, Toast.LENGTH_SHORT).show();
}
}
This code will work for mobile number contacts, not for email or something. i found this code simplest. let me know if there is any problem.
start activity with pick intent on phone data type:
findViewById(R.id.choose_contact_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(pickContact, PICK_CONTACT_REQUEST);
}
});
Now set onAcitivityResult();
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
switch (requestCode){
case PICK_CONTACT_REQUEST:
if (resultCode == RESULT_OK){
Uri contactUri = intent.getData();
Cursor nameCursor = getContentResolver().query(contactUri, null, null, null, null);
if (nameCursor.moveToFirst()){
String name = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
((EditText)findViewById(R.id.person_name)).setText(name);
((EditText)findViewById(R.id.enter_mobile)).setText(number);
nameCursor.close();
}
}
break;
}
}

Categories

Resources