SmsManager.Default.SendTextMessage sends too many sms-xamarin - android

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

Related

Android C#: sending SMS from Android service

I have android service written using Xamarin Studio and I'm trying to send sms message from this service automatically. For sending I use the following code:
SmsManager.Default.SendTextMessage("+7926736XXXX", null, "Simple Service sent you a message", null, null);
As a result SMS didn't sent to other mobile but left in SMS list marked by red triangle. I can send them later by hands without any problem. Can anybody know where may be my error?
Try this,
SmsManager sms = SmsManager.getDefault();
PendingIntent sentPI;
String SENT = "SMS_SENT";
sentPI = PendingIntent.getBroadcast(this, 0,new Intent(SENT), 0);
sms.sendTextMessage("+7926736XXXX", null, message, sentPI, null);
OR
send it in intent like this
var smsUri = Android.Net.Uri.Parse("smsto:7926736XXXX");
var smsIntent = new Intent (Intent.ActionSendto, smsUri);
smsIntent.PutExtra ("sms_body", "Hello from Xamarin.Android");
StartActivity (smsIntent);
hope this will help.
Try checking the SmsResultError and SmsStatus of the sent and delivered intents to determine why your SMS is not working.
Add BroadcastReceiver inner class to your Service:
public YourSMSService : Service
{
const string SentAction = "SentAction";
const string DeliveredAction = "DeliveredAction";
SmsReceiver smsReceiver;
~~~~
class SmsReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action == SentAction)
{
// `Log` it if needed...
Toast.MakeText(context, $"{(SmsResultError)ResultCode}", ToastLength.Long).Show();
}
if (intent.Action == DeliveredAction)
{
// `Log` it if needed...
Toast.MakeText(context, $"{(SmsStatus)ResultCode}", ToastLength.Long).Show();
}
}
}
~~~~
}
Create/Register the BroadcastReceiver (usually done in the Service .actor)
smsReceiver = new SmsReceiver();
RegisterReceiver(smsReceiver, new IntentFilter(SentAction));
RegisterReceiver(smsReceiver, new IntentFilter(DeliveredAction));
Supply sent and delivered PendingIntents to the SendTextMessage:
var sentIntent = PendingIntent.GetBroadcast(this, 0, new Intent(SentAction), 0);
var deliveredIntent = PendingIntent.GetBroadcast(this, 0, new Intent(DeliveredAction), 0);
SmsManager.Default.SendTextMessage("1234567890", null, "Simple Service sent you a message", sentIntent, deliveredIntent);
Now when you send an SMS in your service, you will get a Toast message of the status.
The question is closed. This is the hardware problem of the my phone. I connected my 10 yeas old chinese Samsung Galaxy copy and my application immediately started to work

Android - LocalBroadcastManager for SMS interception not firing

I've got an JavascriptInterface method that registers a LocalBroadcastManager listening to received SMS's. When an SMS arrives the onReceive method doesn't get called.
public void UIregisterSMSSource(String number) {
LocalBroadcastManager.getInstance(this)
.registerReceiver(mMessageReceiver, new IntentFilter(ACTION_SMS_RECEIVE));
}
/**
* SMS Receiver
*/
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_SMS_RECEIVE)) {
StringBuilder buf = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Bundle extras = intent.getExtras();
Object[] pdus = (Object[]) extras.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
String sender = SMessage.getOriginatingAddress();
String body = SMessage.getMessageBody().toString();
Log.d(TAG, "[SMS] onReceive by " + sender + ". Content - " + body);
// Save preferences of the activation code
}
}
}
}
};
This is the ACTION_SMS_RECEIVE variable:
private static final String ACTION_SMS_RECEIVE = "android.provider.Telephony.SMS_RECEIVED";
I've tested this before as BroadcastaReceiver and it worked. I removed the receiver from the manifest too which I don't know if it's right.
Do I need to configure something more? On the examples I've came across there's no further configuration needed.
Thanks in advance.
Receiver mMessageReceiver can only listen the intent which sent via LocalBroadcastManager.sendBroadcast(intent).
Here, intent with android.provider.Telephony.SMS_RECEIVED action, is being raised by system (not using LocalBroadcastManager.sendBroadcast(intent)), so your app does not listen to this intent.
Your previous approach was correct, and you can continue on that logic.
You can read a detailed example of LocalBroadcastManager to make clear your doubts about its working flow.

can i able to send broadcast to default sms application in android?

This is my code:
public class MMSReciever extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
String incomingNumber = null;
if(intent.getAction().equals("android.provider.Telephony.WAP_PUSH_RECEIVED"))
{
Bundle bundle = intent.getExtras();
try
{
if (bundle != null)
{
String type = intent.getType();
if(type.trim().equalsIgnoreCase("application/vnd.wap.mms-message"))
{
byte[] buffer = bundle.getByteArray("data");
incomingNumber = new String(buffer);
int indx = incomingNumber.indexOf("/TYPE");
if(indx>0 && (indx-15)>0)
{
int newIndx = indx - 15;
incomingNumber = incomingNumber.substring(newIndx, indx);
indx = incomingNumber.indexOf("+");
if(indx>0)
{
incomingNumber = incomingNumber.substring(indx);
System.out.println("Mobile Number: " + incomingNumber);
}
}
}
I have bundle data with me when sms arrives to my device .Then can i able to send broadcast with this bundle data to default sms application
If you getting a sms broadcast event, you don't need to send from your side. As the same broadcast will be received by all applications watching for that intent.
Now unless, you want to change something. then you need to cancel the broadcast and make your own broadcast and fire it. and yes you can do that. make sure, your receiver priority is high enough to receive event before other apps do.
Hope this is what you asking.

Android notify last received SMS

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

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