I had my codes sending an sms before. But it just stopped. I have given the app permissions and it still isnt working. Here are my codes below
SmsManager.getDefault().sendTextMessage("5555", null, "Hi there", null, null );
Try this code,
It works for me
SmsManager sms = SmsManager.getDefault();
PendingIntent pendingIntent;
String SENT = "SMS_SENT";
pendingIntent = PendingIntent.getBroadcast(this, 0,new Intent(SENT), 0);
sms.sendTextMessage(phoneNumber, null, message, pendingIntent, null);
Related
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" />
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);
I have an issue with mismatch data between putting data to intent and reading it.
In my service I'm creating notification and adding parameters to the intent.
When i see the notification the paramters is ok. But when I open the notification the parameters act wired. I get true parameters in my system but no the once i sent.
is this problem because I'm creating notification in my service?
public void createNotification(ChatMessage message)
{
Notification notification = new Notification(R.drawable.ic_action_sm,
"Message From" +
" " + message.getSenderName(),
System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this, ChatActivity.class);
intent.putExtra("userProfile", message.getReciverChatProfile());
intent.putExtra("friendProfile", message.getSenderChatProfile());
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this,
message.getSenderName(),
message.getMessage(),
activity);
notification.number += 1;
notification.iconLevel = 3;
notificationManager.notify(0, notification);
}
Adding FLAG_UPDATE_CURRENT solved the problem.
PendingIntent activity = PendingIntent.getActivity(this, 0,intent,PendingIntent.FLAG_UPDATE_CURRENT);