I try to send message using SmsManager in android i use this code in my application it work fine only some of the device it is not working.
i just make one class for send message
public class Sms {
public static final String TAG = "Sms";
public void sendSms(String number, String message) {
Log.i(TAG, "Sending " + message + " to " + number);
if (message.length() > 159) {
message = message.substring(0, 159);
}
SmsManager.getDefault().sendTextMessage(number, null, message, null,null);
}
}
this class is for send message
i am send using below code
buddys = db.getAllContacts();
for (int i = 0; i < buddys.size(); i++) {
Sms sms = new Sms();
sms.sendSms(buddys.get(i).getPhoneNumber(), msg_);
Toast.makeText(context.getApplicationContext(), ""+msg_+" to "+buddys.get(i).getPhoneNumber(), Toast.LENGTH_SHORT).show();
}
numbers are come from database there are maximum 4 number. it is working fine in gionee , sony , and micromax , and Moto g But not working in samsung device and zenfone . i put the toast for test it also toast the message in all the devices but the problem is only not send the message. i reused the size of message to 160 i think that was the problem but again same problem is occurs please help me.
Related
I am writing an app that sends the user of the app a text message when such user performs a certain action with the app. Now, i am using the SmsManager API to achieve this. Only problem is that sent message and received message is showing at the same time in messages. How do i fix this. Also, i want to change the name of the sender of the message to the app name.
public void sendSMSEntering() {
String phone = "08187016641";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phone, null, "Welcome to the ICAN 2019 conference" , null, null);
Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
}
public void sendSMSExiting() {
String phone = "08187016641";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phone, null, "Goodbye. We hope you enjoyed the conference" , null, null);
Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
}
My button from xml onClickListener
sendMeAMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickcount++;
if (clickcount % 2 == 0) {
sendSMSEntering();
} else {
sendSMSExiting();
}
}
});
Screenshot
As can be observed in the image above, the message the app sent and the message received by me are both there. I only want the message received to be displayed in messages. Also you will observe that the sender of the message is my phone number (String phone = "08187016641"). 'phone' is used as both the recipients phone number and the sender of the message. Perhaps smsManager.sendTextMessage(phone, null, "Goodbye. We hope you enjoyed the conference" , null, null); is not good enough to achieved this and needs to be changed to something else. I need suggestions
After so much research I found out that the SMSManager API is whack. There are so many good SMS API's out there that you can integrate with your app. You can then use a GET or POST as appropriate to request SMS messages. These SMS API's give you the option of using a custom Sender name and other powerful options to customize incoming text messages. Some of these SMS API's include Nexmo SMS API, Ozeki SMS API and so many others. The good thing is, they have a comprehensive documentation. Infact, integrating these SMS API's into your app takes just a few lines of code.
I have an auto reply sms Android application I built and I don't want the auto reply (sent sms) to show in the default messaging app. I have searched and searched and couldn't find an answer. Is there a way to bypass writing the sent sms into the default messaging app?
Here my BroadcastReciever I am using to get the data and send out the message
public class SmsReceiver extends BroadcastReceiver {
ParseUser user = ParseUser.getCurrentUser();
// Auto reply message composed of the current reply and url from that business
String msg = user.getString("myCurrentReply") + " " + user.getString("couponUrlChosen");
List smsFromList = user.getList("smsFrom");
String userName = (String) user.get("username");
#Override
public void onReceive(final Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
final String pno = smsMessage[0].getOriginatingAddress();
user.put("lastSmsFrom", pno);
user.saveInBackground();
// show first message
Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
// Check Phone Number from SMS Received against Array in User Row
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
Log.d("Username: ", userName);
query.whereEqualTo("username", userName);
query.whereContainedIn("lastSmsFrom", smsFromList);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> smsList, ParseException e) {
if (e == null) {
Log.d("Errors", "none");
if (smsList.size() == 0) {
// Send SMS
sendSms(pno, msg);
// Add Phone number to smsFrom in currentUsers Row
user.addUnique("smsFrom", pno);
// Save Phone Number in Array
user.saveInBackground();
Log.d("List size: ", " " + smsList.size());
}
} else {
Log.d("Error Message: ",
e.getMessage());
}
Log.d("Already sent to this number today. ", " " + smsList.size());
}
});
}
private void sendSms(String phonenumber, String message) {
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(phonenumber, null, message, null, null);
}
}
Prior to KitKat, SMS sent using SmsManager require the app sending the message to insert it into the Provider, so it would just be a matter of omitting that.
Starting with KitKat, any app that is not the default SMS app and uses SmsManager to send messages will have the messages automatically written to the Provider for it by the system. There's no way to prevent this, and, furthermore, the app won't be able to delete those messages, either, as it won't have write access to the Provider.*
The app that is the default SMS app is responsible for writing its outgoing messages, so it would be able to omit that step. The system does no automatic writes for the default SMS app.
* There is a security hole in 4.4 only, by which a non-default app can gain write access to the Provider. It is detailed in my answer here, but it will not work in versions after KitKat.
I am creating an application that will send/receive messages to/from a certain gateway number. I don't want the messages that are sent to this particular number to be stored in the inbox of my device.
The code I am using to send the SMS is:
protected void sendMessage(String strMessage) {
try {
SmsManager smsMan = SmsManager.getDefault();
smsMan.sendTextMessage(gatewayNumber, null, strMessage, null, null);
Toast.makeText(getApplicationContext(),
"SMS was sent successfully!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS was not sent, please contact developer!",
Toast.LENGTH_LONG).show();
}
}
I have managed to tackle the problem of storing incoming messages by using the abortBroadcast(); function. (I realize this doesn't work above KitKat, and I'm fine with it).
But the messages that are sent from the application are being stored in the inbox too (only on KitKat), and that bothers me. Any solutions?
I am developing an application which needs to send SMS.
I am using SmsManager.SendTextMessage and SmsManager.sendMultipartTextMessage , Both of them are working well , the main problem is sent message will be saved on sent items(only in Kitkat , the older version don't save SMS) , but I don't need to save message.
My app is default messageing app.
What should I do to prevent saving SMS in kitkat?
Here is my code:
try
{
SmsManager smsManager = SmsManager.getDefault();
String body ="Test";
String to = "5556";
ArrayList<String> parts =smsManager.divideMessage(body);
smsManager.sendMultipartTextMessage(to,null, parts, null, null);
}
catch(Exception ex)
{
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
After looking on th e website, I was not able to find any solution to this problem.
Let me explain a bit more :
I'm trying to send (programmatically) from an App a multipart SMS. It's actually working on my device (Nexus 4) but I tried it on a Galaxy Note, which convert any long-sized SMS to MMS and the app only send short SMS. Any clue about that? Does the Samsung API or something prevent the sendMultipartTextMessage() method from being executed?
In my sending function, I have Log debug, which is shown on screen, so the call to this function is well done..
Thanks for any help!
Here is the code I use :
public static void sendSMS(final Activity a, String phoneNumber, String message)
{
try {
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
ArrayList<PendingIntent> sendIntents = new ArrayList<PendingIntent>(parts.size());
for (int i = 0; i < sendIntents.size(); i++)
{
Intent sendInt = new Intent("");
PendingIntent sendResult = PendingIntent.getBroadcast(a.getApplicationContext(), 0, sendInt, PendingIntent.FLAG_ONE_SHOT);
sendIntents.add(sendResult);
}
sms.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
ContentValues values = new ContentValues();
values.put("address", phoneNumber);
values.put("body", message);
a.getApplicationContext().getContentResolver().insert(Uri.parse("content://sms/sent"), values);
} catch(Exception e) {
e.printStackTrace(System.out);
}
}
public void sendSMS(String phoneNumber, String longMessage) {
try {
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(longMessage);
sms.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
} catch (Exception ex) {
ex.printStackTrace();
}
}
add this permission also :
<uses-permission android:name="android.permission.SEND_SMS"/>
above code works for on Samsung Galaxy Note 2 (Android 4.1.1), Micromax HD Canvas (Android 4.1.1) , Samsung Galaxy S3 (Android 4.1.1), HTC One X (Android 4.0).
So this function is not block for me on samsung phones.