Send a SMS via intent
I have developed this functionality from one Blog. There are 2 ways you can send SMS.
- Open native SMS composer
- write your message and send from your Android application
This is the code of 1st method.
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/btnSendSMS"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Send SMS"
android:layout_centerInParent="true"
android:onClick="sendSMS">
</Button>
</RelativeLayout>
Activity
public class SendSMSActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sendSMS(View v)
{
String number = "12346556"; // The number on which you want to send SMS
startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null)));
}
/* or
public void sendSMS(View v)
{
Uri uri = Uri.parse("smsto:12346556");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "Here you can set the SMS text to be sent");
startActivity(it);
} */
}
NOTE:-
In this method, you don’t require SEND_SMS permission inside the AndroidManifest.xml file.
For 2nd method refer to this BLOG. You will find a good explanation from here.
Hope this will help you...
Sending SMS via an Intent and know if the SMS has been sent or not
In the following example, we use a ContentObserver
to monitor updates to the SMS Provider. This Observer is created and started before the SMS Intent is fired, and checks the Provider changes against the destination address. The Activity that creates the Observer must implement the SmsSendObserver.SmsSendListener
interface to receive the callback.
The Observer's constructor includes a timeout
parameter (in milliseconds) to allow the Observer to be properly unregistered if the message is not sent after a reasonable amount of time. This can be set to NO_TIMEOUT
if desired. However, the class, as written, is meant for "one shot" use, and it will unregister itself and nullify members upon callback. The stop()
method can be used to clean up if no callback occurs. In either case, the instance is no longer usable, and any reference to it should be set to null.
Example Activity:
public class MainActivity extends Activity
implements SmsSendObserver.SmsSendListener {
...
private void sendMessage(String phoneNumber, String messageBody) {
// This example has a timeout set to 15 seconds
new SmsSendObserver(this, phoneNumber, 15000).start();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("smsto:" + phoneNumber));
intent.putExtra("address", phoneNumber);
intent.putExtra("sms_body", messageBody);
intent.putExtra("exit_on_sent", true);
startActivity(intent);
}
public void onSmsSendEvent(boolean sent) {
Toast.makeText(this, sent ? "Message was sent" : "Timed out",
Toast.LENGTH_SHORT).show();
}
}
The SmsSendObserver
class:
public class SmsSendObserver extends ContentObserver {
public static final int NO_TIMEOUT = -1;
private static final Handler handler = new Handler();
private static final Uri uri = Uri.parse("content://sms/");
private static final String COLUMN_ADDRESS = "address";
private static final String COLUMN_TYPE = "type";
private static final String[] PROJECTION = { COLUMN_ADDRESS, COLUMN_TYPE };
private static final int MESSAGE_TYPE_SENT = 2;
private Context context = null;
private ContentResolver resolver = null;
private String phoneNumber = null;
private long timeout = NO_TIMEOUT;
private boolean wasSent = false;
private boolean timedOut = false;
public SmsSendObserver(Context context, String phoneNumber, long timeout) {
super(handler);
if (context instanceof SmsSendListener) {
this.context = context;
this.resolver = context.getContentResolver();
this.phoneNumber = phoneNumber;
this.timeout = timeout;
}
else {
throw new IllegalArgumentException(
"Context must implement SmsSendListener interface");
}
}
private Runnable runOut = new Runnable() {
@Override
public void run() {
if (!wasSent) {
timedOut = true;
callBack();
}
}
};
public void start() {
if (resolver != null) {
resolver.registerContentObserver(uri, true, this);
if (timeout > NO_TIMEOUT) {
handler.postDelayed(runOut, timeout);
}
}
else {
throw new IllegalStateException(
"Current SmsSendObserver instance is invalid");
}
}
public void stop() {
if (resolver != null) {
resolver.unregisterContentObserver(this);
resolver = null;
context = null;
}
}
private void callBack() {
((SmsSendListener) context).onSmsSendEvent(wasSent);
stop();
}
@Override
public void onChange(boolean selfChange) {
if (wasSent || timedOut)
return;
Cursor cursor = null;
try {
cursor = resolver.query(uri, PROJECTION, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
final String address =
cursor.getString(cursor.getColumnIndex(COLUMN_ADDRESS));
final int type =
cursor.getInt(cursor.getColumnIndex(COLUMN_TYPE));
if (PhoneNumberUtils.compare(address, phoneNumber) &&
type == MESSAGE_TYPE_SENT) {
wasSent = true;
callBack();
}
}
}
finally {
if (cursor != null) {
cursor.close();
}
}
}
public interface SmsSendListener {
// Passes true if the message was sent
// Passes false if timed out
public void onSmsSendEvent(boolean sent);
}
}
Android : For call and send SMS Intent need to add permission?
You can use this code without any permission:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + phoneNumber));
intent.putExtra("sms_body", message);
startActivity(intent);
For the call, you can use this without any permission:
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:phoneNumber"))
startActivity(intent);
You don't need any permission as the user actually make the call, you just redirect him to the phone app. If you want to make the phone call directly through your app, you have to use ACTION_CALL
instead of ACTION_DIAL
and add the permission :
<uses-permission android:name="android.permission.CALL_PHONE" />
Hope it helps !
How to open sms app via implicit intent?
You can try this, it work for me:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_MESSAGING);
startActivity(intent);
How to send SMS via intent but no recipient
Actually, I found the answer to my own question. The sms:
protocol doesn't actually REQUIRE a phone number.
The following code will bring up a chooser for all apps that support sms messages:
final Intent textIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("sms:?body=" + YOUR BODY));
startActivity(textIntent);
ACTION_SEND used to send sms
Why, this should work fine. http://developer.android.com/reference/android/content/Intent.html#ACTION_SENDTO
Check out my code:
Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);
Related Topics
Android M Camera Intent + Permission Bug
Removing Unused Strings During Proguard Optimisation
Find Out If Listview Is Scrolled to the Bottom
Set the Layout Weight of a Textview Programmatically
How to Force the Action Bar to Be at the Bottom in Ics
Visual Studio (2015) Emulator for Android Not Working - Xde.Exe - Exit Code 3
Get Ssid When Wifi Is Connected
Android Maps API V2 with Custom Markers
Android Fade in and Fade Out with Imageview
How to Add Footer to Navigationview - Android Support Design Library
How to 'Gettopactivity' Name or Get Currently Running Application Package Name in Lollipop
How to Find MAC Address of an Android Device Programmatically
Detecting When User Has Dismissed the Soft Keyboard
How to Make Circle Custom Progress Bar in Android
Creating Tabs Using Fragments Now That Tabactivity Is Deprecated
Converting Yuv->Rgb(Image Processing)->Yuv During Onpreviewframe in Android