Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video we'll see how we can update the widget to receive clicks!
Related Links
-
0:00
We just finished putting together all the pieces that make up a widget but
-
0:04
it doesn't do very much.
-
0:05
Let's see if we can't spice things up a bit by making it so
-
0:08
that when we tap on the widget, it randomly changes color.
-
0:11
Normally, we would be able to do this by adding an OnClickListener to our frame
-
0:16
layout.
-
0:16
But since our views are hiding inside a remote views object,
-
0:20
we're not going to be able to do that.
-
0:22
Instead, what we can do,
-
0:24
is set a pending intent which we will fire off when we click on our frame layout.
-
0:29
And we're going to make that pending intent trigger a call to onUpdate,
-
0:33
where we'll update the color of our widget.
-
0:36
Let's start in our widget provider and add some space between where
-
0:40
we declare our remote views and where we update our widget.
-
0:44
Then we need to create a pending intent but in order to create a pending intent,
-
0:49
we must first create a regular intent.
-
0:51
So let's create a new intent, named intent.
-
0:55
And let's pass in our context for the first parameter, and for the class,
-
1:00
let's pass in our widget provider.
-
1:02
Then on the next line, let's call intent.setAction and
-
1:09
pass in AppWidgetManager.ACTION_APPWIDGET_UPDATE.
-
1:15
Then let's add another line and
-
1:18
let's add our app widget IDs as an extra to this intent.
-
1:22
Intent.putExtra and for the name, let's pass in
-
1:27
appWidgetManager.EXTRA_APPWIDGET_IDS and
-
1:32
let's finish up by passing in appWidgetIds for the value.
-
1:38
Now that we've got our intent,
-
1:39
we just need to use this intent to create a pending intent.
-
1:43
Let's add two lines and then create a new variable to hold our pending intent and
-
1:50
let's set it = PendingIntent.getBroadcast, which takes in four parameters.
-
1:57
I'm going to do these each on a separate line to keep things visible.
-
2:01
The first parameter is the context, which we've already got access to.
-
2:06
Next, we've got the request code, the request code is just a way for
-
2:10
us to differentiate between two similar pending intents.
-
2:13
Pending intents get stored in the Android system, and
-
2:17
they're not stored individually.
-
2:19
If there's an existing pending intent that looks just like yours does,
-
2:23
Android's not going to create another one.
-
2:25
But if we'd like Android to create another pending intent,
-
2:29
we can pass an unique request code to make Android think that this one is different.
-
2:34
Also, if you want to know if Android thinks two pending intent's are the same,
-
2:39
just use the intent.filter= method.
-
2:41
That said, we're fine just passing in zero.
-
2:45
Next, we need to pass in our intent, and finally,
-
2:48
we need to add any flags to specify how to handle our pending intent.
-
2:53
Let's pass in PendingIntent.FLAG_UPDATE_CURRENT.
-
3:00
This way, if our pending intent already exists, then we'll keep it and
-
3:05
we'll update its extra data with the new data in this version.
-
3:09
There's a few other options for flags and if you'd like to read more about them,
-
3:13
there's a link below in the teacher's notes.
-
3:15
All right, last but not least,
-
3:17
we need to attach this pending intent to the frame layout in our widget.
-
3:21
To do this we can just use the set on click pending intent method
-
3:26
on a remote use object.
-
3:28
So on the next line let's type removeViews.setOnClickPendingIntent and
-
3:35
then for the view ID let's pass in our frame layout R.id.frameLayout and
-
3:41
then let's pass in our pendingIntent for the pending intent.
-
3:47
And now, whenever we tap on one of our widgets,
-
3:50
it will trigger a call to this onUpdate method, awesome.
-
3:54
But we still haven't done anything to make it change color and
-
3:57
we'll get to that in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up