How to add parameters into PendingIntent - android

I am very new to Android Programming. I would like to know if it is possible to add extra parameters using PendingIntent ?
For example I wanna do something like intent.putExtra("test", key) in PendingIntent. Is it possible ?

You can add parameters in intent.
Intent intent= new Intent(context, YourActivity.class);
String displayName = "abc"
intent.putExtra("name", displayName);
PendingIntent pendIt = PendingIntent.getActivity(context,
TxrjConstant.REQUEST_READ_NOTIFICATION,
intent, PendingIntent.FLAG_UPDATE_CURRENT);

Related

intent.putExtra() in pending intent not working

I am passing a pending intent through alarmreceiver, from a service class. But, after the pendingIntent fires, the intent.putExtra() information is not being received by the broadcastreceiver class. Here is my code for firing the pendingIntent
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
The alarm receiver class is below
public String msg, phonen;
#Override
public void onReceive(Context context, Intent intent){
Bundle extras = intent.getExtras();
msg = extras.getString("msg");
phonen = extras.getString("phone");
Log.d("onReceive", "About to execute MyTask");
Toast.makeText(context,msg, Toast.LENGTH_LONG).show();
}
The msg information in toast, that is being received from pending intent, is not being shown. Instead, a blank toast is shown.
Try this
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(),
id,
aint,
// as stated in the comments, this flag is important!
PendingIntent.FLAG_UPDATE_CURRENT);
Remember to put an unique id in the PendingIntent constructor, or you could get some weird thing when you try to get the putExtra values.
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(),
UUID.randomUUID().hashCode(),
aint,
PendingIntent.FLAG_UPDATE_CURRENT
);
You have to use getStringExtra() and be sure the String are not null:
Intent intent = getIntent();
msg = intent.getStringExtra("msg");
phonen = intent.getStringExtra("phone");
if(msg!=null){
Toast.makeText(context,msg,
Toast.LENGTH_LONG).show();
}
and reverse Your putExtras before PendingIntent:
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
This is because you are first initialize Intent and add to PendingIntent. After that you are adding info in intent. You should add info to intent then add this intent to PendingIntent.
In my case i was missing PendingIntent.FLAG_UPDATE_CURRENT as flag.
final Intent my_intent = new Intent(this.context , Alarm_Receiver.class);
//put in extra string into my intent
//tell the clock that the user pressed the "alarm on button"
my_intent.putExtra("extra" , 1);
int state = my_intent.getExtras().getInt("extra");
Log.i("extraa" , "the state in the main activity in alarm on Button is: " + state);
I had the same problem and I found out that you should put the putExtra before you involve the Intent
Please Use FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT

Passing a value with PendingIntent

I am using PendingIntent. I need to pass a value. Intent uses putExtra. Is there an equivalent for PendingIntent? If yes, please provide sample example. Thanks in advance.
I am using:
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
Just put the extras in the original intent, i.e.
Intent i = new Intent(context, MainActivity.class);
i.putExtra("key1", "the answer");
i.putExtra("key2", 42);
...
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, i, 0);
The "inner" intent is the one your Activity will actually receive. Check the documentation for PendingIntent.getActivity().
Then, in MainActivity.onCreate():
Intent intent = getIntent();
String strValue = intent.getStringExtra("key1");
int intValue = intent.getIntExtra("key2");
...

PendingIntent and Notification Intent

I find the code below redundant. Am I missing something basic here ? Is there a way to reduce the duplicate code here. Can I use either Intent or PendingIntent object, why both ?
Intent updateUI = new Intent(SENDTOBACKGROUND_SERVICE);
updateUI.putExtra("Signal", God.YELLOW);
sendBroadcast(updateUI);
Intent sendNotification = new Intent(DriverService.this, DriverHome.class);
sendNotification.putExtra("Signal", God.YELLOW);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, sendNotification, PendingIntent.FLAG_UPDATE_CURRENT);
Notification n = new NotificationCompat.Builder(DriverService.this)
.setContentTitle("Attempting to update location")
.setContentText("Cab #(last) " + currentLocationInText)
.setSmallIcon(R.drawable.yellow).setContentIntent(pIntent).setAutoCancel(true)
.build();
((NotificationManager) DriverService.this.getSystemService(NOTIFICATION_SERVICE)).notify("Taxeeta", R.id.cabLocation, n);
No, you need both. A PendingIntent is a wrapper around an Intent. You can't have a PendingIntent without an Intent to wrap. And you can't put an Intent in a Notification. You need to use a PendingIntent when you want to hand over an Intent to another component, so that the other component can send the Intent for you (as a kind of "proxy") at some point in the future.
As explain in the doc
http://developer.android.com/reference/android/app/PendingIntent.html#getActivity%28android.content.Context,%20int,%20android.content.Intent,%20int%29
"Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent)" so you need PendingIntent and Intent.

Two buttons with PendingIntents - Widget

I'm creating a widget with two buttons.
One of them updates the content of the widget and the second one must launch an activity.
I have two PendingIntent for each action, but I can't make them both work. If one works the other one doesn't.
I've revised the code and can't understand what's wrong.
Any help will be very appreciated.
This is the code.
RemoteViews controls = new RemoteViews(context.getPackageName(), R.layout.miwidget);
Intent intent = new Intent("actony.com.ACTUALIZAR_WIDGET");
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
Intent intentSettings = new Intent();
intentSettings.setClass(context,WidgetConfig.class);
PendingIntent pendingIntentUpdate = PendingIntent.getBroadcast(context, widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
controls.setOnClickPendingIntent(R.id.BtnActualizar, pendingIntentUpdate);
PendingIntent pendingIntentSettings = PendingIntent.getActivity(context, 0, intentSettings, 0);
controls.setOnClickPendingIntent(R.id.botonSettings, pendingIntentSettings);
Try adding the getActivity PendingIntent.FLAG_UPDATE_CURRENT aswell...
PendingIntent pendingIntentSettings =
PendingIntent.getActivity(context, 0, intentSettings, PendingIntent.FLAG_UPDATE_CURRENT);
and if multiple widget's are possible add the widgetId there too.
Make sure both of the activities/broadcasts are listed in the manifest file.
Moreover, try creating the Intent with this constructor:
Intent intent = new Intent(context,ACTUALIZAR_WIDGET.class);
Intent intentSettings = new Intent(context,WidgetConfig.class);
add imports if needed.
Hope some of that will make you widget work.
Check this link to know which button has been clicked when there is two or more button in a widget..
https://stackoverflow.com/a/10733049/1331593
It should work... IF it does not work please let me know what is the problem...
You can try this code:
Intent read = new Intent(ctx, NotificationClick.class);
read.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
read.putExtra(Intent.EXTRA_SUBJECT, "READ");
PendingIntent readInt = PendingIntent.getActivity(ctx, 1, read, PendingIntent.FLAG_IMMUTABLE);
Intent reply = new Intent(ctx, NotificationClick.class);
reply.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
reply.putExtra(Intent.EXTRA_SUBJECT, "REPLY");
PendingIntent replyInt = PendingIntent.getActivity(ctx, 2, reply, PendingIntent.FLAG_IMMUTABLE);
NotificationManagerCompat nMgr = NotificationManagerCompat.from(ctx);
Notification newMessageNotification = new NotificationCompat.Builder(ctx, "MESSAGE_CHANNEL")
.setSmallIcon(R.drawable.user_account)
.setContentTitle(contact)
.setContentText(text)
.addAction(R.drawable.drafts, "Read", readInt)
.addAction(R.drawable.drafts, "Reply", replyInt)
.setGroup(MESSAGE_GROUP)
.build();
nMgr.notify(100, newMessageNotification);

Send String from Appwidget to New Activity

I'm working on an appWidget which has a textView, all i want is preview this textView in a new activity that i launch with
Intent configIntent = new Intent(context, ViewFromWidget.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent myPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.widgetLayout, myPendingIntent);
Can some one point me out on how can i send a string to the new activity that u start via appWidget?
That should be easy, you want to use an intent extra for passing strings.
Add this before setAction
configIntent.putExtra("MyKey", myString);
And in the activity getting the intent use getStringExtra
String passedString = getIntent().getStringExtra("MyKey");

Categories

Resources