I am creating my first app and I would like to know if it is possible to have two intents linked to a single button. My goal is: when the button is pressed it sends out an SMS and then makes a call, this is the call method which works I would like to know if there is a way to hard code an SMS and have it sent when the same button is pressed?
public void phone_call(View view) {
String number = "123456789";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
return;
}
startActivity(intent);
}
yes, it is possible, use SmsManager
String number = "123456789";
String message = "some message";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, message, null, null);
... rest of code and finally
startActivity(intent);
note you should have proper permission and also your app must be set as default SMS sender (only one app may send SMS, multiple apps can only receive/print them)
<uses-permission android:name="android.permission.SEND_SMS" />
Related
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.
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...
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);
Create the intent like this:
Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address","your desired phoneNumber");
smsIntent.putExtra("sms_body","your desired message");
smsIntent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(smsIntent);
Try this code. It will work
Uri smsUri = Uri.parse("tel:123456");
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
intent.putExtra("sms_body", "sms text");
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
Hope this will help you.
If you want a certain message, use this:
String phoneNo = "";//The phone number you want to text
String sms= "";//The message you want to text to the phone
Intent smsIntent = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", phoneNo, null));
smsIntent.putExtra("sms_body",sms);
startActivity(smsIntent);
Uri uriSms = Uri.parse("smsto:1234567899");
Intent intentSMS = new Intent(Intent.ACTION_SENDTO, uriSms);
intentSMS.putExtra("sms_body", "The SMS text");
startActivity(intentSMS);
/**
* Intent to Send SMS
*
*
* Extras:
*
* "subject"
* A string for the message subject (usually for MMS only).
* "sms_body"
* A string for the text message.
* EXTRA_STREAM
* A Uri pointing to the image or video to attach.
*
* For More Info:
* https://developer.android.com/guide/components/intents-common#SendMessage
*
* #param phoneNumber on which SMS to send
* #param message text Message to send with SMS
*/
public void startSMSIntent(String phoneNumber, String message) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
// This ensures only SMS apps respond
intent.setData(Uri.parse("smsto:"+phoneNumber));
intent.putExtra("sms_body", message);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Hope this is work, this is working in my app
SmsManager.getDefault().sendTextMessage("Phone Number", null, "Message", null, null);
Add try-catch otherwise phones without sim will crash.
void sentMessage(String msg) {
try {
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("sms_body", msg);
startActivity(smsIntent);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "No SIM Found", Toast.LENGTH_LONG).show();
}
}
Manifest permission (you can put it after or before "application" )
uses-permission android:name="android.permission.SEND_SMS"/>
make a button for example and write the below code ( as written before by Prem at this thread ) and replace the below phone_Number by an actual number, it will work:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", "phone_Number", null)));
i'm trying to send an SMS to a short number from my code:
sendSMS("5556", "Test");
private void sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
After calling the method Android will show the next pop-up:
http://imgur.com/a/mRLYg
How can I know if the user clicked on send or cancel?
See the SmsManager API - use the PendingIntent sentIntent parameter to determine the result.
I'm rather new to Android.
Im trying to send SMS from Android application.
When using the SMS Intent the SMS window opens and the user needs to approve the SMS and send it.
Is there a way to automatically send the SMS without the user confirming it?
Thanks,
Lior
You can use this method to send an sms. If the sms is greater than 160 character then sendMultipartTextMessage is used.
private void sendSms(String phonenumber,String message, boolean isBinary)
{
SmsManager manager = SmsManager.getDefault();
PendingIntent piSend = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT), 0);
PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0);
if(isBinary)
{
byte[] data = new byte[message.length()];
for(int index=0; index<message.length() && index < MAX_SMS_MESSAGE_LENGTH; ++index)
{
data[index] = (byte)message.charAt(index);
}
manager.sendDataMessage(phonenumber, null, (short) SMS_PORT, data,piSend, piDelivered);
}
else
{
int length = message.length();
if(length > MAX_SMS_MESSAGE_LENGTH)
{
ArrayList<String> messagelist = manager.divideMessage(message);
manager.sendMultipartTextMessage(phonenumber, null, messagelist, null, null);
}
else
{
manager.sendTextMessage(phonenumber, null, message, piSend, piDelivered);
}
}
}
Update
piSend and piDelivered are Pending Intent They can trigger a broadcast when the method finish sending an SMS
Here is sample code for broadcast receiver
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String message = null;
switch (getResultCode()) {
case Activity.RESULT_OK:
message = "Message sent!";
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
message = "Error. Message not sent.";
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
message = "Error: No service.";
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
message = "Error: Null PDU.";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
message = "Error: Radio off.";
break;
}
AppMsg.makeText(SendMessagesWindow.this, message,
AppMsg.STYLE_CONFIRM).setLayoutGravity(Gravity.BOTTOM)
.show();
}
};
and you can register it using below line in your Activity
registerReceiver(receiver, new IntentFilter(SMS_SENT)); // SMS_SENT is a constant
Also don't forget to unregister broadcast in onDestroy
#Override
protected void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
If your application has in the AndroidManifest.xml the following permission
<uses-permission android:name="android.permission.SEND_SMS"/>
you can send as many SMS as you want with
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(...);
and that is all.
Yes, you can send SMS using the SmsManager. Please keep in mind that your application will need the SEND_SMS permission for this to work.
Yes, you can send sms without making user interaction...But it works, when user wants to send sms only to a single number.
try {
SmsManager.getDefault().sendTextMessage(RecipientNumber, null,
"Hello SMS!", null, null);
} catch (Exception e) {
AlertDialog.Builder alertDialogBuilder = new
AlertDialog.Builder(this);
AlertDialog dialog = alertDialogBuilder.create();
dialog.setMessage(e.getMessage());
dialog.show();
}
Also, add manifest permission....
<uses-permission android:name="android.permission.SEND_SMS"/>
Hey I'm making an app in which I need to send a text message but every time I send the message the app opens again and all the variables are reset(I have tried to implement a system that save the variables but they still get reset), however it still sends the message. Why is it doing this, and how can I fix it; this is my code
public void sendSMS(String phono, String mes)
{
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(phono, null, mes, pi, null);
}
//Button that uses method
b = (Button) findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
phono = "personal phone number";
if (phono.length() > 0 && mes.length() > 0)
sendSMS(phono, mes);
}
});
You are asking the SMSManager to relaunch your app when the SMS is successfully sent.
From the docs,
public void sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent),
the pi in your code will be used as the sentIntent, which means when the SMS is sent out of the device, SMSManager will automatically trigger the intent.
If you don't want the SMS manager to relaunch your app again after the SMS is sent, just send a null in place of pi.
sm.sendTextMessage(phono, null, mes, null, null);
Replace sm.sendTextMessage(phono, null, mes, pi, null); with
sm.sendTextMessage(phono, null, mes, null, null);
Im trying to start a new activity AFTER the email is sent
I use the following to invoke the default email client
this.startActivity(Intent
.createChooser(emailIntent, "Send mail..."));
This works fine and starts the email app when you send the message it reurns you to the activity it starts from
I want to then start a new activity( the main Screen) and then close the previous one
I inserted the following
Intent myIntent = new Intent(view.getContext(), Activity1.class);
startActivityForResult(myIntent, 0);
finish();
This works but the email screen is hidden behind the new activity and i have to quit the app to send the email
is there a way of starting the activity after the email is sent?
Im using gmail as the client
Any help appreciated
Mark
Unfortunately I didn't find any info that when you start email client with starActivityForResult() it returns with some result code whether user send email or cancel that action. Gmail sent activity is started within your activity stack so you end up starting two activities, gmail app as second.
Hope this will help you
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL,new String[] { "tony#mail.com"});
email.putExtra(Intent.EXTRA_SUBJECT,"Test");
email.putExtra(Intent.EXTRA_TEXT,"sent a message using for testing ");
email.setType("message/rfc822");
startActivityForResult(Intent.createChooser(email, "Choose an Email client:"),
1);
You need onActivityResult method like below
protected void onActivityResult(int requestCode, int
resultCode, Intent data) {
if(requestCode == MY_REQUEST_CODE) {
if(resultCode == RESULT_OK) {
} else {
Intent ingoHome = new Intent(current.this,
newclass.class);
startActivity(ingoHome);
}
}
finish();
}