Android - sendTextMessage Message not showingup in SMS Apps - android

I am writing an app which will send an SMS automatically to a given number on some conditions, SMS code is working and SMS is also delivered.. but the sent SMS are not showing up in any of the other SMS Clients/Apps installed...
String phoneNumber = "1234512345";
String message = "Test Message";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
What am i missing?
I want the sent SMS (sent by my app) to show up in other SMS apps which are installed..

Yes you have sent SMS progrmatically but you haven't informed SMS Content Provider so it will not get your message unless you inform it.
ContentValues values = new ContentValues();
values.put("address", "1234512345");
values.put("body", "Test Message");
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
Make sure to include below permissions in AndroidManifest.xml file:
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />

use broadcast receiver in receiving side application
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
String str="";
String from="";
if (bundle != null) {
Object[] pdus = (Object[])bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += messages[i].getMessageBody().toString();
from = messages[i].getOriginatingAddress().toString();
}
if(from.equals("123456789")){
str= str.trim();
abortBroadcast();
Intent intt = new Intent(context,MainActivity.class);
intt.putExtras("message",str);
intt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intt);
}
}
in your mainActivity write some code to display the obtained message. the message will be in the intent. so get the String from it.
here the broadcast receiver, receives messages from only number "123456789". the messages from this number will not go to inbox of ur mobile. if u want to see in inbox also remove the method abortBroadcast().

Related

Write received message to SMS provider ( API level 19+ )

I am creating an SMS manager for KitKat and later version. I have implemented all the receivers as directed in the official doc by android.
I have to receive the SMS SMS_DELIVER broadcast receiver and read it and then have to write to the SMS provider.
till now I am able to read the SMS received. I have set my app as the default SMS app in the device. I am also parsing the SMS and can see it in the log.
problem
I am unable to write the SMS to the SMS provider.
here is the broadcast receiver:
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdusObj.length];
for (int i = 0; i < messages.length; i++) {
String format = bundle.getString("format");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i], format);
} else {
messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
}
}
for (SmsMessage msg : messages) {
Log.i("log", "display msg body : " + msg.getDisplayMessageBody() + "originating address : " + msg.getDisplayOriginatingAddress() + " get message body : " + msg.getMessageBody());
//here I have to write the message to the sms provider.
}
}
}
}
does anyone have any suggestions? please help me.
Update
i have tried android-kitkat-api-19-how-to-write-messages-in-sms-content-provider-without so question but i am unable to get around it. That solution is for writing to the sent SMS without doing anything like sending the SMS. but I want to write the received SMS here.
Thanks to Mike M. for the help. i got help from this answer - sms-doesnt-save-on-kitkat-4-4-already-set-as-default-messaging-app from SO and this post - kitkat-sms-mms-supports .
here is what i have done :
to write the sms into the sms provider of android system i used content provider. and passed value to it. code snippet is :
ContentValues values = new ContentValues();
values.put(Telephony.Sms.ADDRESS, msg.getDisplayOriginatingAddress());
values.put(Telephony.Sms.BODY, msg.getMessageBody());
context.getApplicationContext().getContentResolver().insert(Telephony.Sms.Sent.CONTENT_URI, values);
this code will save the received sms into the system sms provider and even after your ap is uninstalled other sms app can read it. keep in mind that you need to be the default sms app to do this operation. and you have to provide WRITE_SMS permission in manifest. i have targeted kitkat and versions after it. for previous versions you have to some part of code differently.
the whole SmsReceiver class after completion is :
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdusObj.length];
for (int i = 0; i < messages.length; i++) {
String format = bundle.getString("format");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i], format);
} else {
messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
}
}
for (SmsMessage msg : messages) {
Log.i("log", "display msg body : " + msg.getDisplayMessageBody() + "originating address : " + msg.getDisplayOriginatingAddress() + " get message body : " + msg.getMessageBody());
ContentValues values = new ContentValues();
values.put(Telephony.Sms.ADDRESS, msg.getDisplayOriginatingAddress());
values.put(Telephony.Sms.BODY, msg.getMessageBody());
context.getApplicationContext().getContentResolver().insert(Telephony.Sms.Sent.CONTENT_URI, values);
}
}
}
}
I had the same problem. You have to register a SMSBroadcaseReceiver, MMSBroadcastReceiver, a QuickreplyService in order to be be able to be set as default SMS receiver.
This helped me:
https://www.androidauthority.com/how-to-create-an-sms-app-part-2-724264/

How to open incoming sms from notification directly to my application's intent as textview or listview?

I want to open incoming sms from notification directly to my application's intent , Please help I tried following code to read sms from inbox
public class SmsReceiver extends BroadcastReceiver
{
public static String BODY = "Test";
public static String ADDRESS = "5556";
#Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
ADDRESS=msgs[i].getOriginatingAddress();
BODY=str;
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
When a new SMS is received by your device, the android creates a intent and broadcasts the SMS_RECEIVED_ACTION. What you are going to do is create an Intent Filter for the activity of your application that handles the SMS that the phone receives.
When the phone gets a new SMS message and the user clicks on the notification, she is presented with a dialog of apps that can handle this action*****. Your app should be there, and when she decides that she wants your app to handle the incoming message then you can use the getMessagesFromIntent method in order to extract the message (you use that method inside your app).
PS: You cannot set your app as the default message app, which you probably need to do. The user has to explicitly choose your app to open the message, and if she wants it to be the default messaging app (by ticking the set as default checkbox).
Note: It looks like you have to create a broadcast receiver that receives the SMS_RECEIVED_ACTION which you can use prior to API 19 as a constant at android.provider.Telephony.SMS_RECEIVED and launch your own notifications in which you direct the user directly to the application of yours. Unfortunately it seems that the default messaging app will still issue notifications.
Credit: #Mike M.

cancelling the broadcast of a receiving SMS doesn't work in the device

I have a Broadcast Receiver in my application in which it invokes the abortBroadcast() method. So it cancels the broadcast of a receiving SMS and checks whether a specific content is available. If it is available application perform some tasks, otherwise clearAbortBroadcast() method is invoked to continue the broadcast of the SMS to the inbox. Also I have set a higher value as the priority in the manifest file.
I am using android 2.2 version for developing the application. Everything is working fine with the emulator and it provides the expected results for me. But when I test the application in the phone which has 4.0.4 version (ICS), it doesn't work. Message broadcast cancellation is not happening and every receiving SMS which contains the specific content reaches the inbox as a normal SMS does.
P.S : Everything works fine with the emulator. I have set the priority as high as "999999". All the permissions have been included.
Here is the code snippet.
public class SMSReciever extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
this.abortBroadcast();
final String pinNumber = "abcd";
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String smsMsg = "";
String replyPhoneNum = "";
if(bundle != null)
{
Object[] pdus = (Object[])bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
smsMsg += "SMS From " + msgs[i].getOriginatingAddress();
smsMsg += "\n";
smsMsg += msgs[i].getMessageBody().toString();
smsMsg += "\n";
replyPhoneNum = msgs[i].getOriginatingAddress();
}
}
if(smsMsg.contains("SecretCode: "+pin+"\n"))
{
String[] splitMsg = smsMsg.split("\\n");
String[] splitFeatures;
Bundle b = new Bundle();
splitMsg = smsMsg.split("\\n");
for(int i=0; i<splitMsg.length; i++)
{
if(!(splitMsg[i].equalsIgnoreCase("null")))
{
splitFeatures = splitMsg[i].split(":");
if(splitFeatures[0].equals("Contact Number"))
{
String contactName = splitFeatures[1];
b.putString("contactname", contactName);
}
}
if(splitMsg[i].contains("email"))
{
String email = "email";
b.putString("email", email);
}
}
b.putString("replyPhoneNum", replyPhoneNum);
Intent i = new Intent(context, NextActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtras(b);
context.startActivity(i);
}
else
{
this.clearAbortBroadcast();
}
}
}
In the android manifest file,
<receiver android:name="SmsReceiver">
<intent-filter android:priority="999999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
That's it. All other permissions have been used correctly. This works fine in the emulator. But not working in the device.

How to avoid SMS's from selected phone numbers being made visible to user? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can we delete an SMS in Android before it reaches the inbox?
I've already gotten receive SMS code running successfully. What I cannot determine is a way (or if it's possible) to process messages sent from specific phone numbers within my app without them being made visible to the user. All other SMS's sent from other phone number would be handled by the normal Android SMS processing. I.e., SMS's from selected numbers should not be visible to the phone user and the rest should. Any suggestions?
Here's the SMSReceiver code (taken straight from Wei-Meng Lee's book):
public class SMSReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//get the received SMS message
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null) {
// retrieve the SMS message
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += "\nMessage Text:\n";
str += msgs[i].getMessageBody().toString();
str += "\nLength="+msgs[i].getMessageBody().toString().length()+"\n";
} // [END FOR]
// display the new SMS message
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
// send a broadcast intent to update the SMS received in the activity
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
} // [END IF]
} // [END onReceive]
} // [END SMSReceiver]
well if you put an if before you start concatenating str, you can check the originating address, and compare it to your blacklist of addresses, if its on the list, then simply use the continue to skip the concatenations

can text messages be opened directly on third party app (android)?

when a text message is sent form, lets say an application 'myApp' it'll open in default text message app of the receiver. but i want to control how it looks to receiver(like changing colour). Is there anyway to send text and read that text in native app, 'myApp'? Or identify it was sent from 'myApp' and import message to 'myApp'.
sure you can to receive messages make a broadcast receiver for ingoing messages an each time a message arrive start your activity that displays the message ...
public class SMSApp extends IntentReceiver {
private static final String LOG_TAG = "SMSApp";
/* package */ static final String ACTION =
"android.provider.Telephony.SMS_RECEIVED";
public void onReceiveIntent(Context context, Intent intent) {
if (intent.getAction().equals(ACTION)) {
StringBuilder buf = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
for (int i = 0; i < messages.length; i++) {
SmsMessage message = messages[i];
buf.append("Received SMS from ");
buf.append(message.getDisplayOriginatingAddress());
buf.append(" - ");
buf.append(message.getDisplayMessageBody());
}
}
//start you messages activity
Intent i = new Intent();
i.setClassName("com.test", "com.test.myMessagesAcivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//prepare message text to be sent to the activity via bundle
Bundle bundle = new Bundle();
bundle.putString("message", but.toString());
i.putExtras(bundle);
context.startActivity(i);
}
}
}
and in your manifest file add these permissions
<uses-permission android:id="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
and this receiver
<receiver class="SMSApp">
<intent-filter>
<action android:value="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
and to send SMS from your app
use this method
public void eb3atSMS(String phoneNumber, String message)
{
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, **DummyClasshere.class**), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
}

Categories

Resources