I want to send an SMS via intent, but when I use this code, it redirects me to a wrong contact:
Intent intentt = new Intent(Intent.ACTION_VIEW); intentt.setData(Uri.parse("sms:")); intentt.setType("vnd.android-dir/mms-sms"); intentt.putExtra(Intent.EXTRA_TEXT, ""); intentt.putExtra("address", phone number); context.startActivity(intentt);
Why?
Also, I know a way to follow SMS sending, but I do not know how code this:
Starting activity: Intent { act=android.intent.action.SENDTO dat=smsto:%2B**XXXXXXXXXXXX** flg=0x14000000 cmp=com.android.mms/.ui.ComposeMessageActivity }
where XXXXXXXXXXXX is phone number.
Built-in SMS applicationIntent sendIntent = new Intent(Intent. ACTION_VIEW); sendIntent. putExtra("sms_body", "default content"); sendIntent. setType("vnd.
An intent allows you to start an activity in another app by describing a simple action you'd like to perform (such as "view a map" or "take a picture") in an Intent object.
An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents.
An Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use cases: Starting an activity. An Activity represents a single screen in an app.
I have developed this functionality from one Blog. There are 2 ways you can send SMS.
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...
Uri uri = Uri.parse("smsto:YOUR_SMS_NUMBER"); Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.putExtra("sms_body", "The SMS text"); startActivity(intent);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With