Saturday, 17 May 2014

How To Send Sms & Types Of Possibilities

Hi!

  In android there are two way to send a SMS,

  The very 1st one is by calling the SmsManager with out intent send message as  programatically, without displaying as message box,

  The another one is by calling the intent, user can edit the message before sending it like a normal message

  Optionally we can set the delivered report too

// i call the default "Intent.ACTION_GET_CONTENT" intent to pick the number to send sms.

@Override

public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.Way1:
Intent intent_1 = new Intent(Intent.ACTION_GET_CONTENT);
intent_1.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent_1, 1);

break;
case R.id.Way2:
Intent intent_2 = new Intent(Intent.ACTION_GET_CONTENT);
intent_2.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent_2, 2);

break;
case R.id.Way3:
Intent intent_3 = new Intent(Intent.ACTION_GET_CONTENT);
intent_3.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent_3, 3);

break;
default:
break;
}
}

// By calling the OnActivityResult() method we can split the action by based on the RequestCode


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver()
.query(uri,
new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE },
null, null, null);

if (c != null && c.moveToFirst()) {
String number = c.getString(0);
if (requestCode == 1) {
way_1(number);
}
if (requestCode == 2) {
way_2(number);
}
if (requestCode == 3) {
way_3(number);
}

}
} finally {
if (c != null) {
c.close();
}
}

}

}
}

// By calling the SmsManager


private void way_1(String number) {
// TODO Auto-generated method stub
Toast.makeText(this,
"Please wait! \n Message Sending to " + ": " + number,
Toast.LENGTH_LONG).show();

String phoneNo = "+91" + number;
String message = "Hi! " + "\n" + "This is Test Message";
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "Message Sent Successfuly",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Message Sending faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}

//  By Calling the Intent with Delivery report


private void way_2(String number) {
Toast.makeText(this,
"Please wait! \n Message Sending to " + ": " + number,
Toast.LENGTH_LONG).show();

String phoneNo = "+91" + number;
String message = "Hi! " + "\n" + "This is Test Message";
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";

PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);

PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);

registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(
getBaseContext(),
"SMS sent! \n Please wait till SMS Reach Your Partner ..,",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(
getBaseContext(),
"SMS Not sent! \n  Generic failure Please Try Again",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(),
"SMS Not sent! \n  No service Please Try Again",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(),
"SMS Not sent! \n  Null PDU Please Try Again",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(),
"SMS Not sent! \n  Radio off Please Try Again",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(),
"SMS delivered Successfuly", Toast.LENGTH_SHORT)
.show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS is Not delivered",
Toast.LENGTH_SHORT).show();

break;
}
}
}, new IntentFilter(DELIVERED));

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNo, null, message, sentPI, deliveredPI);
}


// By Calling the intent without Delevery Report


private void way_3(String number) {
Toast.makeText(this,
"Please wait! \n Message Sending to " + ": " + number,
Toast.LENGTH_LONG).show();
String phoneNo = "+91" + number;
String message = "Hi! " + "\n" + "This is Test Message";
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("smsto:"));
smsIntent.setType("vnd.android-dir/mms-sms");

smsIntent.putExtra("address", new String(phoneNo));
smsIntent.putExtra("sms_body", message);
try {
startActivity(smsIntent);
Log.i("Message Sent Successfuly..,", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Welcome.this,
"Message Sending faild, please try again..,",
Toast.LENGTH_SHORT).show();
}

}



call the default "Intent.ACTION_GET_CONTENT" intent



By calling the SmsManager



By Calling the intent



Thank You!

Please Leave Your Comment..,


Have A Happy Day..,