Android sending SMS with value back to a mobile automatically ( in background) - android

After My android app is able to retrieve a token from a web application, I would like to know how to make it send the token through a SMS to a normal mobile device ( e.g. Iphone, Nokia) ?

Please try following code,
public class SendSMS
{
public static void sendsms(String address,String msgContent)
{
try
{
SmsManager sms = SmsManager.getDefault();
ArrayList<String> smsString = sms.divideMessage(msgContent);
sms.sendMultipartTextMessage(address, null, smsString, null, null);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Related

How to send hidden SMS on android 6.0+ [duplicate]

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.

How to prevent Outgoing SMS from getting stored in 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?

Send SMS with out saving it in sent item in kitkat

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();
}

Sending multipart SMS from device converting into MMS

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.

onChange method of ContentObserver not called when SMS message is read on Android

I was looking quite a bit on the forums for determining how many SMS messages I have that were not read. The code below seems to work good for received messages, but once I actually read the message the onChange of the ContentObserver is not being called.
So, on incoming SMS message the onChange is getting called and I get the correct number, but after reading the message it is not being called. The device is Samsung Note running Android 2.3.6 not rooted. Any help will be appreciated.
public class UnreadSMSContentObserver extends ContentObserver {
private static final String myTAG="PhoneInfoSMS";
private static final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
private ContentResolver mContentResolver;
private Handler mHandler;
public int unreadSMS;
private static final String TAG = "PhoneInfoSMS";
public UnreadSMSContentObserver(ContentResolver cr, Handler h) {
super(null);
mContentResolver = cr;
mHandler = h;
}
public int getUnreadSMS() {
if (mContentResolver != null) {
try {
Cursor c = mContentResolver.query(SMS_INBOX, null, "read = 0", null, null);
if (c != null) {
unreadSMS = c.getCount();
c.deactivate();
Log.d(TAG, unreadSMS + " unread SMS messages");
}
}
catch (Exception ex) {
Log.e("ERROR: " + ex.toString(), "");
}
}
return unreadSMS;
}
#Override
public void onChange(boolean selfChange) {
Log.d(myTAG, "onChange");
if (mContentResolver != null) {
getUnreadSMS();
mHandler.obtainMessage(PhoneInfoServer.CONTENTO_INFOCHANGED, PhoneInfoServer.CONTENTO_US, unreadSMS).sendToTarget();
Log.d(myTAG, "done");
}
}
}
In the manifest file I have the following permissions:
"android.permission.READ_CONTACTS"
"android.permission.READ_SMS"
"android.permission.GET_ACCOUNTS"
"android.permission.BATTERY_STATS"
"android.permission.BLUETOOTH"
"android.permission.BLUETOOTH_ADMIN"
"android.permission.BROADCAST_SMS"
"android.permission.RECEIVE_SMS"
"android.permission.BROADCAST_SMS"
"android.permission.RECEIVE_MMS"
The way I set it up on the calling service is:
in the onCreate of the service I do:
mContentResolver = this.getContentResolver();
mUnreadSMSContentObserver = new UnreadSMSContentObserver(mContentResolver, mContentObserversHandler);
in the onStartCommand of the service I do:
mContentResolver.registerContentObserver(Uri.parse("content://sms/"), true, mUnreadSMSContentObserver);
mContentResolver.registerContentObserver(Uri.parse("content://sms/inbox/"), true, mUnreadSMSContentObserver);
I tried only the content://sms/ and only content://sms/inbox/ and both... did not solve the problem.
P.S. Similar method for the missed calls works perfect!
The application+service is sending over Bluetooth the battery data, missed calls and unread SMS messages to an Arduino based device that displays it on an LED screen. Why? Two reasons: first, this is my first Android program made for fun. The second is I am not carrying the phone with me at home all the time, and it is typically in my home office. If I did not hear it ringing I will still be able to see the LED display telling me I had calls or SMS when passing by.
this solution has worked for me :
context.getContentResolver().
registerContentObserver(Uri.parse("content://mms-sms/"), true, smsObserver);

Categories

Resources