Android appwidget, can i put buttons on it? - android

I would like to add twoo buttons on an appwidget, then i would like to set the onclicklistener methods of this.
Is there a way to do it? how?

RemoteViews views = new RemoteViews(context.getApplicationContext(), R.layout.main_view);
Intent intent = new Intent(context, InputBroadcastReceiver.class); // receiver class
intent.putExtra(EXTRA_ID, id); // some extras
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT); views.setOnClickPendingIntent(R.id.my_button, pendingIntent);
Then override the onReceive method in InputBroadcastReceiver and enjoy your inputs ;)
BroadcastReceivers are explaining here.

Related

How can i go to my App when i click on AppWidget

How can i go to my App when i click on AppWidget
I saw no. of questions how to create widget in android home,but after craeting widget how to go to my app while clicking on that i didn't get from anyone of that.Please give me the guidelines to achieve this.
Thanks,
Since it is an AppWidget setting an onClickListener won't work. Instead you have to set an onClickPendingIntent on a specific view.
See the following code for the necessary steps (you should put this code into your onUpdate() method):
// Inflate the Widget layout
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.your_widget_layout);
// Create and set the Intent
Intent intent = new Intent(context, MyClass.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
// Add intent to a view (like a button)
remoteViews.setOnClickPendingIntent(R.id.your_button, pendingIntent);
Please note: The code only shows the relevant parts
As greenapps say :
Add a listener in your widged's view (e.g : click)
Create an intent and put extra information (e.g witch item is clicked)
Then start your activity.
e.g
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//...
Intent intent = new Intent(context, YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
//...
}
}

Implement setOnClickPendingIntent from notification

I build custom notification that contain button and i want to listin when user press on it.
The button should not open any activity but only logic staff like change song.
the Code:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setTextViewText(R.id.toptext, nowPlayingTitle);
//this not work
Intent intent = new Intent(this, receiver.class);
intent.putExtra("UNIQ", "1");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_CANCEL_CURRENT)
contentView.setOnClickPendingIntent(R.id.imageButtonPlay,
pendingIntent);
notification.contentView = contentView;
// this is to return to my activity if click somwhere else in notification
Intent notificationIntent = new Intent(this, MYACTIVITY.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
mNotificationManager.notify(NOTIFICATION_ID, notification);
I don't get the hang of the setOnClickPendingIntent what need to be in the second param?
How can i just call a function after user press on the button?
im probably missing something cause i dont understand the receiver side and what happend after user press
You are missing the fact that the button you created actually doesn't belong to your application. It is created in another context, in another process. There is no way it can call your function.
Instead, when the user taps the button, that pending intent is fired. You can catch it by your receiver (in your activity), check some parameters and do the action.
Or you can implement a service and handle this intent in background. I'd prefer this way.
thanks for quick answer. I try using receiver but it never fired.
The code is in the main question and i created for the reciever class the following code:
public class receiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
}
}
but click on the notification never fire the receiver ( Test on debug mode )

Intent extras not removed/replaced

I am setting an alarm on a button click.
The alarm is triggered with an intent.
This intent get an extra "int" to pass to the Broadcast Receiver.
The problem is that the intent's extra gets set once on the first
click of the button and never changes on the other clicks:
Intent intent = new Intent(A.this, B.class);
intent.putExtra(WAKEUP_DURATION, wakeUpDuration);
PendingIntent sender = PendingIntent.getBroadcast(A.this, 0, intent, 0);
I tried removing it in the Broadcast Receiver, but no luck:
intent.removeExtra(A.WAKEUP_DURATION);
Use FLAG_UPDATE_CURRENT when creating your PendingIntent to update the extras from a new Intent.
Thanks! That did the trick.
For those of you who would like to know the exact answer.
The "FLAG_UPDATE_CURRENT" goes as the fourth argument in the method 'getBroadcast'.
It should look like this:
PendingIntent sender = PendingIntent.getBroadcast(A.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

WidgetProvider Intent extras sent with button click not found in onReceive

I am trying to detect when widget button is clicked but none of the Intent extras are showing up in the onReceive method.
onReceive gets called with every click but none of my Intent extras show up.
My code is below: I only hook up the toggle button in on update so not sure if this is correct. None of the extras show up and categories are null even though I set this.
onUpdate(Context context etc):
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.my_widget);
Intent buttonIntent = new Intent(context, MyWidgetProviderClass.class);
buttonIntent.setAction(ACTION_WIDGET_RECEIVER);
buttonIntent.putExtra("BUTTON_CLICKED", "buttonClick");
buttonIntent.putExtra("BUTTON",899);
PendingIntent muPendingIntent = PendingIntent.getBroadcast(context, 0,
buttonIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
buttonIntent.addCategory("buttonclick");
remoteViews.setOnClickPendingIntent(R.id.ToggleImageButton, myPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
onReceive():
intent.getIntExtra("BUTTON",-1); ---> 1
intent.getCategories() --- > null
Try FLAG_UPDATE_CURRENT instead of FLAG_CANCEL_CURRENT.
Also, your code may have a typo: you have muPendingIntent instead of myPendingIntent.
Also also, please do not use buttonclick as a category. Please namespace it (e.g., com.something.whatever.buttonclick), or remove it, as I am not sure why you would need it.
Here is a sample project demonstrating an app widget that, on a click, triggers an update on itself, with an extra (used to supply the app widget IDs).
Android Apparently Does not like re-use of the name ACTION_WIDGET_RECEIVER and removes those parameters. Created another ACTION just for toggle button, registered in the manifest and now the parameters show up.
I found that if the Intent that was used to create the Pending intent has any extras already in it then the new intent's extras are ignored. For example, if you follow the sample in the Android docs for building a Widget like so
Intent toastIntent = new Intent(context, StackWidgetProvider.class);
toastIntent.setAction(StackWidgetProvider.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
rv.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent);
Then the line
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
Will prevent your new intent's extras from sticking. I removed that line and my new intent worked.

android widget question

can anybody give example how to implement click event in appwidget in android ?
Thanks
You can use RemoteViews.setOnClickPendingIntent(R.id.view_id, intent) method for handle click event of app-widget. For more information you can refer below link
Click here
Use RemoteViews.setOnClickPendingIntent(R.id.view_id, intent) method.
The intent parameter should be a well formed Intent object which would match on of the filters at your manifest file.
Intent intent = new Intent(context, Activity.class);
Uri widgetId = Uri.parse("" + appWidgetId); // this line means
intent.setData(widgetId); // you can send a widget id
PendingIntent pintent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.clickButtoninWidget, pintent);
return views;

Categories

Resources