send SMS- pending intent/put extras? - android

i'm using the code
private void sendSms(String phoneNo, String message){
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, MainScreen.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNo, null, message, pi, null);
}
i have utilized the onSaveInstanceState to save values such as text and spinner position, which will keep things saved and consistent while receiving intents, navigating away from the activity, etc
not sure if i can put extras in the pending intent, so that when it starts my main activity, i can use those extras. because somehow, when the pending intent goes through, i need it to restore to the way it was before the intent was called

You can.. but you must clear the previous pending intent or use the one shot flag while constructing your pending intent.. like this
Intent sentIntent = new Intent(SENT_SMS_ACTION);
sentIntent.putExtra(MESSAGE_ID_TAG, messageId);
PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this, 0, sentIntent, PendingIntent.FLAG_ONE_SHOT);

Related

pending intent in smsmanager

In the messaging program I made, I used pending intent to receive the message sending and delivery report. When I send several messages and wait for the delivery report, the program does not recognize which message the received pending intent belongs to.
My code is:
sentSMS = PendingIntent.getBroadcast(this, msgId(), new Intent(SMS_SENT), 0); deliverSMS = PendingIntent.getBroadcast(this, msgId(), new Intent(SMS_DELIVERED), 0);
According to what I searched for, I have to use the request code parameter for this, for each message I have assigned a request code **(the ID of each message from the database) **in pending intent, but I don't know how to receive it in on receive.
thanks
Also i tried this:
Intent deliveryIntent = new Intent();
deliveryIntent.putExtra("id", msgId());
deliverSMS = PendingIntent.getBroadcast(this, msgId(), deliveryIntent, 0);
//************
Intent sendIntent = new Intent();
sendIntent.putExtra("id", msgId ());
sentSMS = PendingIntent.getBroadcast(this, msgId(), sendIntent, 0);
But I did not receive anything in extra
TThanks 🙏
When you create the PendingIntent, you need to specify PendingIntent.UPDATE_CURRENT as the last parameter of the getBroadcast() call. This will ensure that each PendingIntent has the correct "id".
In onReceive() you should get the broadcast Intent and it should contain the "extra" named "id" with the message ID.
However, if you create the Intent without an ACTION and without a component or package name, your onReceive() will never get called. Please edit your question and show how you have set up your BroadcastReceiver.

Getting Toast Message from Notification

Is it possible to have a Toast being shown when I click on one of the Variants of Notification? I tried to sending this Toast by intent to another class but could not find a way to do so.
Also, i have tried just creating intent to another class which just has a function of creating a Toast message upon being called, but this did not work because the Toast was being shown upon creation of Notification. Please help. Thanks.
Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setTicker("Info")
.setContentTitle("Info ")
.setContentText("NotiText")
.addAction(R.drawable.ic_stat_name, "This should make Toast upon clicking", pendingIntentCall)
.setSmallIcon(R.drawable.ic_stat_name).setAutoCancel(true);
Your intent does nothing. When passing the Intent to PendingActivity.getActivity(...), you must define it as an explicit intent for an Activity. For example:
Intent intent = new Intent(this, SomeActivity.class);
But if you only want to show a toast message, it would be probably better not to start an activity, because that would be quite an expensive operation for that purpose. Instead, you can create a PendingIntent also for a service or a broadcast. For example:
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
And MyBroadcastReceiver.java could look like this (basically):
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context.getApplicationContext(), "Your message", Toast.LENGTH_SHORT).show();
}
}
Btw, it is recommended to add an action or extra to the intent when you create it, and check for that action or extra when receiving the intent to verify that it's actually the intent that you expected to receive.

Keeping track of sms sent in Android

I'm noticing the tracking pending intents that I send out via the standard SmsManager in Android doesn't seem to retain the extra information in them. Example:
Intent sentIntent = new Intent(SENT);
sentIntent.putExtra("value1", "foo"); // <- note this value
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent, 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(numberToSendTo, null, mMessageToSend, sentPI, null);
//... in the broadcastReceiver that catches the SENT intent ...
public void onReceive(Context arg0, Intent arg1) {
arg1.getExtras().getString("value1"); // <- nothing, no such key
}
Can someone test this out, was this behaviour intended and I am doing it wrong, or is this a bug to be filed for Android?
Try adding the flag FILL_IN_SELECTOR when you create the PendingIntent (see the spec for PendingIntent.getBroadcast for the flags and their general behavior). This should force the PendingIntent to take in all the top-level extras from the Intent.

Android PendingIntent Extra

I'm trying to send SMS messages from an Android App. I'm using PendingIntent to so I can use a Broadcast Receiver to check if it was sent ok. As the sendTextMessage call will be done per SMS, I need to send some "extra" data to identify the actual SMS, so as my broadcast receive can do some work on a specific SMS Message.
Here is my sending code:
String SENT = "SMS_SENT";
Intent sentIntent = new Intent(SENT);
sentIntent.putExtra("foo", "BAR");
PendingIntent sentPI = PendingIntent.getBroadcast(baseContext, 0,
sentIntent, 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(baseContext, 0,
new Intent(DELIVERED), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(smsMessage.getNumber(), null, smsMessage.getText(), sentPI, deliveredPI);
The problem is that in my BroadCast receiver, it can't seem to read my Extra "foo":
public class SMSSentBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent intent) {
String smsID = intent.getStringExtra("foo");
......
}
}
smsID just becomes null.
My broadcast receiver is registered like so:
baseContext.registerReceiver(new SMSSentBroadcastReceiver(), new IntentFilter("SMS_SENT"));
Given that the intent filter is working, what am I doing wrong? Why aren't the extras sent as well?
Any help is appreciated. Thanks
What I suspect has happened (at least, it's happened to me), is that you're using the SMS_SENT intent somewhere else prior, whereby at that time, that intent doesn't have "foo" in the extras. Calling PendingIntent.getBroadcast with the same intent and request code will return you the original pending intent.
If it's actually meant to be the same intent, you need to use something like a PendingIntent.FLAG_UPDATE_CURRENT to update the intent with the new extras. If it's not suppose to be the same intent, it should have a different request code, so that it doesn't match the other intent (which is why feeding in a unique request code worked).

Custom status bar notification

I got a custom status bar notification. There is a "Play" and "Next" image on it.
I want to send a Broadcast Intent to a Service when the "play" or "next" image is pressed.
notification = new Notification(R.drawable.ic_launcher, title + "\n"
+ text, System.currentTimeMillis());
contentIntent = PendingIntent.getActivity(context, 0, actionOnClick, 0);
notification.contentIntent = contentIntent;
contentView = new RemoteViews(context.getPackageName(),
R.layout.custom_player_notification);
contentView.setTextViewText(R.id.custom_player_notification_title,
title);
contentView.setTextViewText(R.id.custom_player_notification_text, text);
notification.contentView = contentView;
This is the code.
The ContentView offers the possibility to set "setOnClickPendingIntent" and "setOnClickFillInIntent". How can I use them to send this broadcast?
I want to send a broadcast because I dont want to launch an activity.
I want to send a Broadcast Intent to a Service when the "play" or "next" image is pressed
I would suggest that you want to send a command via startService() rather than a broadcast.
How can I use them to send this broadcast?
You can try calling setOnClickPendingIntent(), supplying a PendingIntent you create via the static getBroadcast() method on PendingIntent (for a broadcast) or getService() (for a command to be sent via startService()).
Bear in mind that none of this will work on most Android devices available at present. I think buttons-in-Notifications works starting on Android 4.0, perhaps 3.0, but definitely not before then.
You could put something similar to this in your onClick() method of the notification, directing it to a broadcast receiver.
Intent intent = new Intent(this, receiver.class);
intent.putExtra("SomeInfoMaybe", info);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_CANCEL_CURRENT);
And then the code for handling the next stage could go in here:
public class receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
System.out.println("SomeInfoMaybe = " + bundle.getString("info"));
//etc...
}
}
calling .notify(); on your notification should help link to the broadcast receiver

Categories

Resources