How to start an activity in a widget button - android

I have the following code so far:
// Login Button
Intent loginIntent = new Intent(this.getApplicationContext(), LoginActivity.class);
loginIntent.putExtra("Source", "widgetLogin");
PendingIntent loginPIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, loginIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
returnView.setOnClickPendingIntent(R.id.widget_login_button_login, loginPIntent);
return returnView;
However when I click on it nothing happens? I am sure I am just missing something with my intent.

Change
PendingIntent.getBroadcast(getApplicationContext(), 0, loginIntent, Intent.FLAG_ACTIVITY_NEW_TASK)
into
PendingIntent.getActivity(getApplicationContext(), 0, loginIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

Related

How do I pass data from widgets with multiple PendingIntents?

Intent intent1 = new Intent(context, RecipeDetailActivity.class);
Intent intent2 = new Intent(context, RecipeDetailActivity.class);
Intent intent3 = new Intent(context, RecipeDetailActivity.class);
Intent intent4 = new Intent(context, RecipeDetailActivity.class);
intent1.putExtra("id", (long) 1);
intent1.putExtra("name", "Nutella Pie");
intent2.putExtra("id", (long) 2);
intent2.putExtra("name", "Brownies");
intent3.putExtra("id", (long) 3);
intent3.putExtra("name", "Yellow Cake");
intent4.putExtra("id", (long) 4);
intent4.putExtra("name", "Cheesecake");
PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 1, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 1, intent2, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntent3 = PendingIntent.getActivity(context, 1, intent3, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntent4 = PendingIntent.getActivity(context, 1, intent4, PendingIntent.FLAG_CANCEL_CURRENT);
views.setOnClickPendingIntent(R.id.tv_recipe1, pendingIntent1);
views.setOnClickPendingIntent(R.id.tv_recipe2, pendingIntent2);
views.setOnClickPendingIntent(R.id.tv_recipe3, pendingIntent3);
views.setOnClickPendingIntent(R.id.tv_recipe4, pendingIntent4);
EDIT: I put different request codes and used OnNewIntent method in the activity. Now works.
I have 4 views in a widget. If I press on the first one, I should open the activity with id = 1, but if second, then open the activity with id = 2, and so on.
views is a RemoteViews object. The RecipeDetailActivity will act on showing data depending on the intent data (id and name). That activity contains a fragment, and when created, set the data to the fragment, then the fragment will update its information async.
It's not working, as clicking on all of them will yield the same result (currently at id = 3). Shouldn't they cancel each other so that when I open a new one give a new intent (or open a completely new activity)?

Several PendingIntent.getService for Android Wear notification

I'm trying to make notification actions for Android Wear,
And I set an PendingIntent (.getService) for every action,
But only the first Action actually work.
My code (variables names are examples):
intent1 = new Intent(this, intent1.class);
Intent intent2 = new Intent(this, intent2.class);
Intent intent3 = new Intent(this, intent3.class);
pendingIntent = PendingIntent.getService(this, 0, intent1, 0);
pendingIntent2 = PendingIntent.getService(this, 0, intent2, 0);
pendingIntent3 = PendingIntent.getService(this, 0, intent3, 0);
Only clicking on the "pendingIntent" action, it workes.
When clicking on "pendingIntent2" or "pendingIntent3" action, they don't work.
Can you help? Thanks.

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");
...

Duplicate MainActivity When Enter From Notification

Intent intent = new Intent(_context, MainActivity.class);
PendingIntent activity = PendingIntent.getActivity(_context, 0, intent, 0);
notification.contentIntent = activity;
Enter MainActivity from notification if MainActivity is already opened, thus I need exit twice. Is this a falg problem, what should I do?
Yes It is.. You have to add this flag to your pending Intent.
Intent intent = new Intent(_context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent activity = PendingIntent.getActivity(_context, 0, intent, 0);
Taken form here,
https://stackoverflow.com/a/7308940/603744

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

Categories

Resources