I have homescreen widget with ListView
How can I obtain number of current Item which was clicked:
I've tried to do it by following way:
Insite custom class which extends
AppWidgetProvider
public void onUpdate(Context ctxt, AppWidgetManager mgr,
int[] appWidgetIds) {
for (int i=0; i<appWidgetIds.length; i++) {
Intent svcIntent=new Intent(ctxt, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews widget=new RemoteViews(ctxt.getPackageName(),
R.layout.widget);
widget.setRemoteAdapter(appWidgetIds[i], R.id.contacts,
svcIntent);
Intent clickIntent=new Intent(ctxt, AppWidget.class);
clickIntent.setAction(ACTION_WIDGET_REFRESH);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds[i]);
PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0 , clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
widget.setPendingIntentTemplate(R.id.contacts, pi);
mgr.updateAppWidget(appWidgetIds[i], widget);
}
super.onUpdate(ctxt, mgr, appWidgetIds);
}
#Override
public void onReceive(Context context, Intent intent) {
Log.i("gotcha","receive");
if (intent.getAction().equals(ACTION_WIDGET_REFRESH)) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
Bundle extras = intent.getExtras();
int extrass= -1;
if(extras!=null) {
extrass = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_IDS);
}
Log.i("receive", Integer.toString(extrass) ) ; // Here is I've expected to see Item ID
}
But It's not helped. I've just see different numbers (not Items ID)
How can I obtain them?
the problem is
PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0 , clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Because if you always give 0 as requestCode, and the flag is FLAG_UPDATE_CURRENT, all the list items refer to the same PendingIntent which is update by the lasted.
So just give different requestCode to the PendingIntent will solve this problem, please use
PendingIntent pi=PendingIntent.getBroadcast(ctxt, appWidgetIds[i] , clickIntent, PendingIntent.FLAG_UPDATE_CURRENT)`
instead.
And also set the OnClickFillIntent to the RemoteView in the adapter.getViewAt()
RemoteViews rv = new RemoteViews(pkg, R.layout.item);
Intent i = new Intent().putExtra("position", position);
rv.setOnClickFillInIntent(R.id.item, i);
Then you can retrieve the appwidget id , as well as the list item position.
Read it once I think you get what should we do
When using collections (eg. ListView, StackView etc.) in widgets, it is very costly to set PendingIntents on the individual items, and is hence not permitted. Instead this method should be used to set a single PendingIntent template on the collection, and individual items can differentiate their on-click behavior using setOnClickFillInIntent(int, Intent).
Related
The Problem
I've implemented this code AppWidgetListView and I've tried to attach clicks.
What's going well: so, clicks from buttons from widget but outside the ListView, works. You can see the click result from that buttons on OnReceive method from a class (WordWidget) that implemented AppWidgetProvider.
what does not work: when you click an item (or an element from that item) from ListView, it doesn't show a thing.
Attempts:
Example 1
Intent configIntent = new Intent(context, typeof(WordWidget));
configIntent.PutExtra("appWidgetId", ids[i]);
PendingIntent configPendingIntent = PendingIntent.GetActivity(context, 0, configIntent, 0);
remoteViews.SetOnClickPendingIntent(Resource.Id.button, configPendingIntent);
Example 2
Intent active = new Intent(context, typeof(WordWidget));
active.SetAction("aaa");
PendingIntent actionPendingIntent = PendingIntent.GetBroadcast(context, 0, active, 0);
remoteViews.SetOnClickPendingIntent(Resource.Id.button, actionPendingIntent);
Example 3
var intent = new Intent(context, typeof(WordWidget));
intent.SetAction(AppWidgetManager.ActionAppwidgetUpdate);
intent.PutExtra(AppWidgetManager.ExtraAppwidgetIds, ids);
var piBackground = PendingIntent.GetBroadcast(context, 0, intent, PendingIntentFlags.UpdateCurrent);
remoteViews.SetOnClickPendingIntent(Resource.Id.button, piBackground);
Example 4
var intent = new Intent(context, typeof(WordWidget));
intent.SetAction("test");
remoteViews.SetOnClickPendingIntent(Resource.Id.button, PendingIntent.GetBroadcast(context, 0, intent, 0));
Example 5
Intent intent = new Intent(Intent.ActionView, Android.Net.Uri.Parse("https://google.com"));
remoteViews.SetOnClickPendingIntent(Resource.Id.button, intent);
I've tried to replace typeof(WordWidget) with typeof(MainActivity) and it does not work. and I've tried to put that codes on public RemoteViews GetViewAt(int position) from ListProvider where the item layout is built and it does not work.
As a test, I implemented the following function: When clicking on the ListView, enter the main page of the APP.
You can refer to the following code:
We need to call SetOnClickFillInIntent to differentiate the item on-click behavior.
In ListProvider.cs
public RemoteViews GetViewAt(int position)
{
RemoteViews remoteView = new RemoteViews(
context.PackageName, Resource.Layout.list_row);
ListItem listItem = listItemList[position];
remoteView.SetTextViewText(Resource.Id.heading, listItem.heading);
remoteView.SetTextViewText(Resource.Id.content, listItem.content);
// add code here
Bundle extras = new Bundle();
extras.PutInt("position", position);
Intent fillInIntent = new Intent();
fillInIntent.PutExtras(extras);
// Make it possible to distinguish the individual on-click
// action of a given item
remoteView.SetOnClickFillInIntent(Resource.Id.item_frame, fillInIntent);
return remoteView;
}
Note: Here item_frame is the id of root element of list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/item_frame"
android:background="#android:color/darker_gray" >
In WidgetProvider.cs
Here,we set the pending intent in OnUpdate method
public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
base.OnUpdate(context, appWidgetManager, appWidgetIds);
int N = appWidgetIds.Length;
for (int i = 0; i < N; i++)
{
RemoteViews remoteViews = updateWidgetListView(context, appWidgetIds[i]);
// we add code here
Intent activityIntent = new Intent(context, typeof(MainActivity));
// activityIntent.SetPackage(context.PackageName);
// Set the action for the intent.
// When the user touches a particular view.
//activityIntent.PutExtra(AppWidgetManager.ExtraAppwidgetId, appWidgetIds[i]);
//activityIntent.SetData(Android.Net.Uri.Parse(activityIntent.ToUri(Android.Content.IntentUriType.AndroidAppScheme)));
PendingIntent pendingIntent = PendingIntent.GetActivity(context, appWidgetIds[i], activityIntent, PendingIntentFlags.UpdateCurrent);
remoteViews.SetPendingIntentTemplate(Resource.Id.listViewWidget, pendingIntent);
appWidgetManager.UpdateAppWidget(appWidgetIds[i], remoteViews);
}
}
I'm creating an android widget that by clicking, every instance of the widget will go to a different url.
I'm having a problem sending the url from the 'onUpdate' to the 'onReceive' method.
The onUpdate code:
List<String> urls = Arrays.asList("google.com", "yahoo.com", "bing.com", "msn.com");
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
int cnt = 0;
// Get all ids
ComponentName thisWidget = new ComponentName(context,MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
// create some random data
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
Log.w("WidgetExample", urls.get(cnt));
// Set the text
remoteViews.setTextViewText(R.id.urlData, urls.get(cnt));
// Register an onClickListener
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
intent.putExtra("url",urls.get(cnt));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.open, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
cnt++;
}
}
The onReceive code:
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
String TAG = "onReceive";
Bundle extras = intent.getExtras();
if(extras!=null) {
String url = extras.getString("url");
Log.d(TAG, "url is : "+url);
}else {
Log.d(TAG, "no url");
}
}
The problem is that i allwas get the same url (the last one in the list - 'msn.com').
Thank's allot
Avi
I think that this append because you override everytime the intent storage in PendingIntent because the requestCode doesn't change.
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,**0**, intent, PendingIntent.FLAG_UPDATE_CURRENT);
If you want set more PendingIntent you must change the requestCode (0 in your case)
Try
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, cnt, intent, PendingIntent.FLAG_UPDATE_CURRENT);
In this way all PendingIntents are different...
You're using PendingIntent.FLAG_UPDATE_CURRENT which, as the documentation states, "[...]if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent[...]"
So since you're trying to create multiple instances of the same class (PendingIntent) that flag is causing the unwanted behaviour. It is updating the previously instantiated object (the first time you called PendingIntent.getBroadcast(...) method) and changes the field(s) of that object and returns it. So that all your calls end up with the last extra (last URL you supplied) .
I'm using the RemoteViewsService.RemoteViewsFactory (link)
to display a listview in a homescreen widget. Everything works fine and I can capture the clicks on the listview items. Now I want to add a button above the listview to allow the user to jump to a config activity:
This is my widget layout xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dip"
android:background="#drawable/widget_frame"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ListView android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:layout_marginLeft="0dp"/>
</LinearLayout>
This is my onUpdate method within the WidgetProvider.java class:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.w(LOG, "onUpdate method called");
for (int i=0; i<appWidgetIds.length; i++) {
Intent svcIntent=new Intent(context, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
// http://stackoverflow.com/questions/13199904/android-home-screen-widget-remoteviews-setremoteadapter-method-not-working
Random generator = new Random();
int randomNumber = generator.nextInt(1000);
svcIntent.putExtra("random", randomNumber);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews remoteviews=new RemoteViews(context.getPackageName(), R.layout.widget_layout);
//------------------------------------------------------------------------------------
// trying to capture the button click ... ?
RemoteViews btn_remoteviews=new RemoteViews(context.getPackageName(), R.id.button1);
Intent btn_clickIntent = new Intent(context, Config.class);
btn_clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
//PendingIntent btn_pendingIntent = PendingIntent.getBroadcast(context, appWidgetIds[i], btn_clickIntent, 0);
PendingIntent btn_pendingIntent = PendingIntent.getActivity(context, 0, btn_clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
btn_remoteviews.setPendingIntentTemplate(R.id.button1, btn_pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], btn_remoteviews);
//------------------------------------------------------------------------------------
remoteviews.setRemoteAdapter(appWidgetIds[i], R.id.listview, svcIntent);
Intent clickIntent=new Intent(context, LoremActivity.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
PendingIntent clickPI=PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteviews.setPendingIntentTemplate(R.id.listview, clickPI);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteviews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
How do I capture the button click ???
All the clicks on my listview are captured but I can't capture the click on the button just above the listview ???
Thanks a lot for your help!
As stated in the page provided by you:
This AppWidgetProvider defines only the onUpdate() method for the
purpose of defining a PendingIntent that launches an Activity and
attaching it to the App Widget's button with
setOnClickPendingIntent(int, PendingIntent). Notice that it includes a
loop that iterates through each entry in appWidgetIds, which is an
array of IDs that ide
Also:
As described in Using the AppWidgetProvider Class, you normally use
setOnClickPendingIntent() to set an object's click behavior—such as to
cause a button to launch an Activity. But this approach is not allowed
for child views in an individual collection item (to clarify, you
could use setOnClickPendingIntent() to set up a global button in the
Gmail app widget that launches the app, for example, but not on the
individual list items). Instead, to add click behavior to individual
items in a collection, you use setOnClickFillInIntent(). This entails
setting up up a pending intent template for your collection view, and
then setting a fill-in intent on each item in the collection via your
RemoteViewsFactory.
What about that example from the link that you provided? I didn't read that page carefully (you definitely should), but shouldn't you setOnClickPendingIntent() or setOnClickFillInIntent()?:
public class ExampleAppWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
This is what I ended up doing ...
for (int i=0; i<appWidgetIds.length; i++) {
Intent svcIntent=new Intent(context, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
// http://stackoverflow.com/questions/13199904/android-home-screen-widget-remoteviews-setremoteadapter-method-not-working
Random generator = new Random();
int randomNumber = generator.nextInt(1000);
svcIntent.putExtra("random", randomNumber);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews remoteviews=new RemoteViews(context.getPackageName(), R.layout.widget_layout);
//------------------------------------------------------------------------------------
// trying to capture the button click ... ?
Intent btn_clickIntent = new Intent(context, Config.class);
btn_clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
PendingIntent btn_pendingIntent = PendingIntent.getActivity(context, 0, btn_clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteviews.setOnClickPendingIntent(R.id.button1, btn_pendingIntent);
Intent btn2_clickIntent = new Intent(context, Config2.class);
btn2_clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
PendingIntent btn2_pendingIntent = PendingIntent.getActivity(context, 0, btn2_clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteviews.setOnClickPendingIntent(R.id.button2, btn2_pendingIntent);
remoteviews.setRemoteAdapter(appWidgetIds[i], R.id.listview, svcIntent);
Intent clickIntent=new Intent(context, LoremActivity.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
PendingIntent clickPI=PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteviews.setPendingIntentTemplate(R.id.listview, clickPI);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteviews);
}
I have implemented a widget with an ImageButton and a TextView. That ImageButton launch an activity when its clicked. This activity updates the widget text with what the user writes on the activity EditText. Now the problem is that I only know how to get the ids like this:
for (int widgetId : allWidgetIds) {
// Create some random data
int number = (new Random().nextInt(100));
RemoteViews remoteViews = new RemoteViews(getApplicationContext()
.getPackageName(), R.layout.widget_layout);
Log.w("WidgetExample", String.valueOf(number));
//Here I should set text from edit text, but I'm using a random for testing.
remoteViews.setTextViewText(R.id.textView1,
"Random: " + String.valueOf(number));
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
This code will obviously change the data of all the ids, since its inside a for. Is there anyway that I can past the clicked widgetId with my intent, so I can eliminate that for? This is my widgetProvider:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// Get all ids
ComponentName thisWidget = new ComponentName(context, Widget.class);
allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
Intent i = new Intent(context, WidgetSetup.class);
i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
i.setFlags(i.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
// Create some random data
int number = (new Random().nextInt(100));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
// Set the text
remoteViews.setTextViewText(R.id.textView1, String.valueOf(number));
Intent active = new Intent(context, Widget.class);
active.setAction(ACTION_WIDGET_REFRESH);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.imageButton1, actionPendingIntent);
active = new Intent(context, Widget.class);
active.setAction(ACTION_WIDGET_SETTINGS);
actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.imageButton2, actionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
}
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_WIDGET_REFRESH)) {
//Log.i("onReceive", ACTION_WIDGET_REFRESH);
Intent i = new Intent(context, MainActivity.class);
i.setFlags(i.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
} else if (intent.getAction().equals(ACTION_WIDGET_SETTINGS)) {
//Log.i("onReceive", AppWidgetManager.EXTRA_APPWIDGET_ID);
Intent i = new Intent(context, WidgetSetup.class);
i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
//Here I tried to pass the widgetID with no luck.
//i.putExtra(pass widget id?);
i.setFlags(i.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
} else {
super.onReceive(context, intent);
}
}
Thanks
For a running example have a look at my code for MiniCallWidget lines 97 and 169.
Add an Extra to the intent you launch onClick and create in the for loop that specifies the ID of the current widget being setup.
Then retrieve the ID from the extras in the receiving Activity, and pass it back when you're done. Then use the returned ID to make changes only to one widget.
You can do this by having an if-else that also checks for a flag that tells whether or not you're updating only one widget.
Would like a button in my widget to fire the APPWIDGET_UPDATE intent on the widget class to force an update, but I dont see APPWIDGET_UPDATE as a static field in Intent.
Is this possible, and how would one do this?
Intent intent = new Intent(context, BaseWidgetProvider.class);
intent.setAction({APPWIDGET_UPDATE INTENT HERE})
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.MyWidgetButton, pendingIntent);
Yes, it's possible. You'll find the action in AppWidgetManager:
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
Edit: You will need to provide the ids of the widgets you want to update. Below is a complete sample.
AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
ComponentName widgetComponent = new ComponentName(context, YourWidget.class);
int[] widgetIds = widgetManager.getAppWidgetIds(widgetComponent);
Intent update = new Intent();
update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);
update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
context.sendBroadcast(update);
I know this is a very old question, but I think this might be interesting, because Android updated the AppWidgets refresh policies. I think this change could prevent the exising answer to work as expected.
This is my solution, using RemoteViews and a collection.
public static final String ACTION_WIDGET_UPDATE = "com.yourpackage.widget.ACTION_UPDATE";
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_WIDGET_UPDATE)) {
int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
AppWidgetManager.getInstance(context)
.notifyAppWidgetViewDataChanged(widgetId, R.id.widgetColectionRoot);
}
super.onReceive(context, intent);
}
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int widgetId : appWidgetIds) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
RemoteViews collectionRemoteView = getRemoteViews(widgetId, context);
appWidgetManager.updateAppWidget(widgetId, collectionRemoteView);
}
}
}
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#SuppressWarnings("deprecation")
private RemoteViews getRemoteViews(int widgetId, Context context) {
// Sets up the intent that points to the RemoteViewService
// that will
// provide the views for this collection.
Intent widgetUpdateServiceIntent = new Intent(context,
RemoteViewsService.class);
widgetUpdateServiceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
// When intents are compared, the extras are ignored, so we need
// to embed the extras
// into the data so that the extras will not be ignored.
widgetUpdateServiceIntent.setData(
Uri.parse(widgetUpdateServiceIntent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews collectionRemoteView = new RemoteViews(context.getPackageName(),
R.layout.widget_collection);
collectionRemoteView.setRemoteAdapter(widgetId,
R.id.widgetColectionRoot, widgetUpdateServiceIntent);
collectionRemoteView.setEmptyView(R.id.widgetColectionRoot, R.id.widgetEmpty);
// This section makes it possible for items to have
// individualized behavior.
// It does this by setting up a pending intent template.
// Individuals items of a collection
// cannot set up their own pending intents. Instead, the
// collection as a whole sets
// up a pending intent template, and the individual items set a
// fillInIntent
// to create unique behavior on an item-by-item basis.
Intent selectItemIntent = new Intent(context,
BrochuresWidgetProvider.class);
Intent refreshIntent = new Intent(selectItemIntent);
refreshIntent.setAction(ACTION_WIDGET_UPDATE);
PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(
context, 0, refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
collectionRemoteView.setOnClickPendingIntent(R.id.widgetReload,
refreshPendingIntent);
return collectionRemoteView;
}
Of course, you also need to register that intent-filter on your manifest, inside your widget provider declaration.