Android notify last received SMS - android

I'm trying to make an simple application to intercept all SMS received from my telephone operator's e-mail service.
How it works: I have an e-mail from my telephone operator's that notifies me every time that an e-mail comes to the inbox with a SMS. The SMS comes that way:
You have a new e-mail from:email#domain.com See it now through internet! Visit http://m.iclaro.com.br. Subject: SUBJECT GOES HERE
This app that i'm trying to make have to intercept these SMS, retrieve the entire subject ("SUBJECT GOES HERE") and send a fake SMS from a number with only the subject on its contents.
What I've already done: intercept all these SMS from this e-mail service, retrieve the subject and fake a new SMS from a new number (I've choosen 3) just with the subject.
But now I have a problem: this new faked SMS doesn't show any notification.
Here goes the BroadcastReceiver:
public class SmsReceiver extends BroadcastReceiver
{
...
public void onReceive( Context context, Intent intent )
{
...
if(address.contains("1") && body.contains(replace))
{
body = body.substring(body.lastIndexOf(replace),body.length());
body = body.replace(replace, "");
address = "3";
ContentResolver contentResolver = context.getContentResolver();
ContentValues values = new ContentValues();
values.put("address", address);
values.put("body", body);
contentResolver.insert(Uri.parse("content://sms/inbox"), values);
this.abortBroadcast();
}
}
}
I had also tried to:
if(address.contains("1") && body.contains(replace))
{
this.abortBroadcast();
and
contentResolver.insert(Uri.parse("content://sms/inbox"), values);
this.abortBroadcast();
and
this.clearAbortBroadcast();
contentResolver.insert(Uri.parse("content://sms/inbox"), values);
this.abortBroadcast();
Is there anyway to re-notify the last received SMS? Any suggestions?

You will have to encode pdu after editing the received sms message. For that you can use java libraries like smslib etc. for encoding pdu.
public class SmsReceiver extends BroadcastReceiver
{
...
public void onReceive( Context context, Intent intent )
{
...
if(address.contains("1") && body.contains(replace))
{
body = body.substring(body.lastIndexOf(replace),body.length());
body = body.replace(replace, "");
address = "3";
//ContentResolver contentResolver = context.getContentResolver();
//ContentValues values = new ContentValues();
//values.put("address", address);
//values.put("body", body);
//contentResolver.insert(Uri.parse("content://sms/inbox"), values);
this.abortBroadcast();
//create new pdu from the edited data
byte[] pdu = .......;
intent.putSerializableExtra("pdus", pdu);
context.sendBroadcast(intent);
}
}
}

Related

Android- read incoming messages using broadcast receiver

I have created an android application that listens for incoming sms. The issue i am encountering is that it also reads previous sms. The goal of the app was to grab sms from a specific originating address and store it in a database.
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.i(TAG, "Intent Received: "+intent.getAction());
if (intent.getAction()==SMS_RECEIVED){
Bundle dataBundle = intent.getExtras();
if(dataBundle != null){
//creating PDU protocol Data unit object which is a protocol for transferring message
Object[] mypdu = (Object[])dataBundle.get("pdus");
final SmsMessage[] message = new SmsMessage[mypdu.length];
for(int i =0; i< mypdu.length; i++){
//for build version >= API
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
String format = dataBundle.getString("format");
//From PDU we get all object and smsMessage using following line of code
message[i] = SmsMessage.createFromPdu((byte[])mypdu[i],format);
}else{
message[i] = SmsMessage.createFromPdu((byte[]) mypdu[i]);
}
msg += message[i].getMessageBody().toString().replace("null","");
originatingAddress = message[i].getOriginatingAddress();
}
msg = msg.replace("null","");
if(originatingAddress.trim().equals("MPESA")) {
Toast.makeText(context.getApplicationContext(), "message: " + msg, Toast.LENGTH_SHORT).show();
}
}
}
// throw new UnsupportedOperationException("Not yet implemented");
}
}
Please try with below way
(1)The Simple way you can achieve to store the unique of record on every SMS is timestamp is only unique in this case whenever you get SMS store timestamp of every Sms as a Unique or primary key as you want in database, like system.currentTimeMillisecond to get time of current SMS and store as LONG type Column in your database,
(2) you can also check with unique time on every SMS get but it is complex to check with every existing records
Hope this process will help in your way with prevent of duplicate record store in database

What is the right way to receive and store an sms in an android default sms app?

My android app's primary purpose is to delete sms messages easily. Since from kitkat, there is a requirement that the app has to be the "default sms app", I ended up creating an app that has all the necessary receivers.
On the SMS Receiver, currently the app is receiving the sms and saving it, with the following code.
public override void OnReceive(Context context, Intent intent)
{
try
{
if (intent.Action != IntentAction) return;
//context.SendOrderedBroadcast(intent, IntentAction);
var bundle = intent.Extras;
if (bundle == null) return;
var pdus = bundle.Get("pdus");
var castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle);
var msgs = new SmsMessage[castedPdus.Length];
var sb = new StringBuilder();
String sender = null;
for (var i = 0; i < msgs.Length; i++)
{
var bytes = new byte[JNIEnv.GetArrayLength(castedPdus[i].Handle)];
JNIEnv.CopyArray(castedPdus[i].Handle, bytes);
msgs[i] = SmsMessage.CreateFromPdu(bytes);
if (sender == null)
sender = msgs[i].OriginatingAddress;
sb.Append(string.Format("SMS From: {0}{1}Body: {2}{1}", msgs[i].OriginatingAddress, System.Environment.NewLine, msgs[i].MessageBody));
putSmsToDatabase(context, msgs[i]);
Toast.MakeText(context, sb.ToString(), ToastLength.Long).Show();
}
}
catch (Exception ex)
{
Toast.MakeText(context, ex.Message, ToastLength.Long).Show();
}
}
private void putSmsToDatabase(Context cntxt, SmsMessage sms)
{
ContentValues values = new ContentValues();
values.Put("ADDRESS", sms.OriginatingAddress);
values.Put("DATE", sms.TimestampMillis);
values.Put("READ", 0);
values.Put("STATUS", sms.Status);
values.Put("TYPE", 1);
values.Put("SEEN", 0);
values.Put("BODY", sms.MessageBody);
// Push row into the SMS table
Android.Net.Uri inboxURI = Android.Net.Uri.Parse("content://sms/inbox");
var uri = cntxt.ContentResolver.Insert(inboxURI, values);
}
This code seem to save it, but the problem is that none of the other sms apps shows these messages stored from my app. Everywhere that i searched for SMS receivers, saving part code is not really provided. Just the receiving the sms and the Toast display is provided. This created the below question.
As a default sms app, is it really necessary that my app should save the sms to "content://sms/inbox"?
If yes, What is wrong with the save shown in the above sample. I have a vague understanding that i may have something to do with conversation/threads. But, since i'm getting started with raw android development, probably i have understood it completely wrong. Tried creating the thread using thread_ids, which doesn't seem to work either.
This piece of the puzzle is stopping me from the roll out of the app. Please help.
All column names should be lowercased as it appears that the Sms ContentResolver is constructing "quoted" column strings within the sql statement.
While the statements:
values.Put("ADDRESS", sms.OriginatingAddress);
and
values.Put("address", sms.OriginatingAddress);
Should resolve to a SQL statement that is case insensitive, it is not.
Thus you should provide the columns names as returned from Telephony.TextBasedSmsColumns (which are all lowercase):
Your code becomes:
ContentValues values = new ContentValues();
values.Put("address", sms.OriginatingAddress);
~~~
If your app is targeting a min. of API19:
ContentValues values = new ContentValues();
values.Put(Telephony.TextBasedSmsColumns.Address, sms.OriginatingAddress);
~~~
The SMS ContentProvider was not publicly documented until API 19(?):
Telephony.TextBasedSmsColumns: TextBasedSmsColumns

SmsManager.Default.SendTextMessage sends too many sms-xamarin

I have an app which is an activity and a broadcast receiver which is have registered it in the manifest and the activity is destroyed at some point.
What does this broadcast receiver do is that it listens to incoming SMS and if the sender is the same as a number (which the user specifies in the activity) it sends an empty SMS to that number (both numbers are the same). I have compiled it with Android 2.3 but there are 2 problems.
1 - The application sends too many messages to that number after receiving one message.
2 - The abortbroadcast() is not working.
This is my broadcast receiver:
namespace SmsBroadcastReceiver
{
[BroadcastReceiver]
[IntentFilter(new string[] { "android.provider.Telephony.SMS_RECEIVED" }, Priority = Int32.MaxValue)]
public class SmsReceiver : BroadcastReceiver
{
public override void OnReceive (Context context, Intent intent)
{
ISharedPreferences pref = PreferenceManager.GetDefaultSharedPreferences (context);
string number = pref.GetString ("Number", "0");
Bundle b= intent.Extras;
var pdus = b.Get("pdus");
var castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle);
var bytes = new byte[JNIEnv.GetArrayLength(castedPdus[0].Handle)];
JNIEnv.CopyArray(castedPdus[0].Handle, bytes);
SmsMessage msg= SmsMessage.CreateFromPdu (bytes);
if (msg.OriginatingAddress == number) {
//send empty sms
SmsManager.Default.SendTextMessage (msg.OriginatingAddress, null, "Empty", null, null);
InvokeAbortBroadcast ();
}
}
}
}
Remove
SmsManager.Default.SendTextMessage (msg.OriginatingAddress, null,"Empty", null, null);
this should fix your problem.
if (msg.OriginatingAddress == number) {
//send empty sms
//Remove /Comment below statement from your code.`enter code here`
SmsManager.Default.SendTextMessage (msg.OriginatingAddress, null, "Empty", null, null);
InvokeAbortBroadcast ();
}

Android - sendTextMessage Message not showingup in SMS Apps

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().

Start intent from BroadcastReceiver class

My application receives SMS and changes to activity to display an alert dialog box in my app. Toast is working well, but it will not change the activity. onReceive() takes SMS that contain email and depending upon that email id my app searches the associated contact number and sends it back in a reply message.
public void onReceive( Context context, Intent intent )
{
// Get SMS map from Intent
Bundle extras = intent.getExtras();
String messages = "";
if ( extras != null )
{
// Get received SMS array
Object[] smsExtra = (Object[]) extras.get( "pdus" );
// Get ContentResolver object for pushing encrypted SMS to incoming folder
//ContentResolver contentResolver = context.getContentResolver();
for ( int i = 0; i < smsExtra.length; ++i )
{
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
String body = sms.getMessageBody().toString();
String address = sms.getOriginatingAddress();
messages += "SMS from " + address + " :\n";
messages += body + "\n";
// Here you can add any your code to work with incoming SMS
// I added encrypting of all received SMS
}
// Display SMS message
Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
Intent i=new Intent(context,AlertActivity.class);
// context.startActivity(i);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
you are adding addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) after starting AlertActivity Activity. use this way :
Intent i=new Intent(context,AlertActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

Categories

Resources