Development of AppWidget (desktop widget) for Android

What is an AppWidget

AppWidget is a desktop widget, also known as desktop control, which is a small program that can be directly displayed on the desktop of Android system. First look at the figure:

In the figure, I use the yellow arrow to indicate the AppWidget. Some frequently used programs can be made into appwidgets, which can be used conveniently. Typical programs include clock, weather, music player, etc. AppWidget is a part of the application development level of Android system. It has special uses. If it is used properly, it will really add color to the app. Its working principle is a method of embedding the control of one process into the window of another process. Long press the blank space of the desktop, and an AppWidget folder will appear. Find the corresponding AppWidget in it, and long press and drag it out to add the AppWidget to the desktop,

How to develop AppWidget

AppWidget is controlled in the form of broadcastreceiver. The main class for developing AppWidget is appwidgetprovider, which inherits from broadcastreceiver. In order to implement desktop widgets, developers only need to develop a subclass inherited from appwidgetprovider and rewrite its onupdate () method. Generally speaking, rewriting this method can be carried out according to the following steps:

1. Create a remoteviews object that specifies the interface layout file of the desktop widget when loaded.

2. Set the properties of each element in the layout file loaded when remoteviews is created.

3. Create a componentname object

4. Call appwidgetmanager to update the desktop widget.

Let's take a practical example, using the example automatically generated by Android studio. (Note: I use the latest version of as 2.2.3, hereinafter referred to as as as.)

Create a new HelloWorld project, and then create an AppWidget named myappwidget provider. According to the default next step, you have completed the development of the simplest AppWidget. After running the program, add the widget to the desktop. The operation steps and default effects are as follows:

Let's see what code as automatically generates for us? Let's take a look at the above steps.

First, there is a class of myappwidgetprovider.

This class inherits from appwidgetprovider. By default, as helps us rewrite onupdate() method, traverse appwidgetids, and call updateapwidget() method. Let's look at the updateappwidget () method. It's very simple. There are only four lines:

In the first line, charsequence widgettext = context. GetString (r.string. Appwidget_text); Declared a string;

In the second line, remoteviews views = new remoteviews (context. Getpackagename(), r.layout. My_app_widget_provider);

A remoteviews object is created. The first parameter is passed to the application package name, and the second parameter specifies the layout file loaded by remoteviews. This line corresponds to the first point in the above steps. You can see that under the RES / layout / directory, as automatically generates a my_ app_ widget_ Provider.xml file, as follows:

This file is the last desktop widget we saw. There is only one textview in the layout file. This is what you might ask. Can you add pictures? Yes, just like the normal activity layout, add ImageView. Smart, you may start to want to customize the style of widgets and add powerful and beautiful custom controls. Unfortunately, you can't. The components that can be added to the widget layout file are limited, and the details will be discussed later when remoteviews is introduced.

The third line, views. Settextviewtext (r.id.appwidget_text, widgettext);

Assign the string declared in the first line to the textview in the above layout file. Note that when assigning values here, specify the ID of the textview and correspond to it. This line is for the second point in the above step.

The fourth line, appwidgetmanager. Updateappwidget (appwidgetid, views);

Here, the appwidgetmanager. Updateappwidget () method is called to update the widget. This line corresponds to the fourth point in the above step.

At this time, you may have questions. The above clearly says four steps. The third step is to create a componentname object, which is obviously unnecessary. Indeed, it is not used in this example. If we tap the code in step 4, the intelligent prompt of as will tell you that appwidgetmanager. Updateappwidget () has three overloaded methods. The three methods in the source code are not written together. For convenience, I copy and post the introduction in the official API here

All three methods receive two parameters, and the second parameter is a remoteviews object. The first parameter of the first method is the componentname object, which updates all AppWidget instances provided by all AppWidget providers. The second method updates the object set of AppWidget with specified ID. the third method updates an AppWidget object with specified ID. Therefore, we generally use the first method. For all AppWidget objects, we can also selectively update them as needed.

Here, all the steps are over, is it over? not yet. As mentioned earlier, the custom myappwidgetprovider inherits from the appwidgetprovider, and the appwidgetprovider inherits from broadcastreceiver,

Therefore, myapp widget provider is essentially a broadcast receiver. It belongs to one of the four components and needs to be registered in our manifest file. Open the androidmanifest.xml file and you can see that the widget is indeed registered, as follows:

There is an action in the above code. This action must be added and cannot be changed. It belongs to the system specification and exists as the ID of the widget. If not, the receiver will not appear in the widget list. Then you see that the widget specifies @ XML / my_ app_ widget_ provider_ Info as meta data, you can find that an XML folder is created under the RES / directory, and a new my folder is created below_ app_ widget_ provider_ Info.xml file, as follows:

The basic information of some widgets is configured here. The common attributes are initiallayout, which is the initialization layout of the widget. Minheight defines the minimum height of the widget, previewimage specifies the preview of the widget in the widget list, and updateperiodmillis specifies the widget update cycle, in milliseconds. For more properties, you can view the API documentation.

At this point, the above extremely simple widget development process is really over. In order to develop more powerful widgets, we also need to learn more about remoteviews and AppWidget provider.

AppWidget makeup - remoteviews

Here are some classes related to remoteviews.

1.1 RemoteViews

Remoteviews, which literally means that it is a remote view. Is a remote view, which is displayed in other processes, but can be updated in another process. The usage scenarios of remoteviews in Android mainly include: Custom notification bar and desktop widget.

In the constructor of remoteviews, the second parameter receives a layout file to determine the view of remoteviews; Then, we call the set method in remoteviews to set each component in the layout. For example, we can call settextviewtext() to set the text of the textview component.

As mentioned earlier, there are restrictions on the components that can be added to the widget layout file. It can support four types of layouts: FrameLayout, LinearLayout, relativelayout, GridLayout, and 13 types of Views: analogclock, button, chrome, imagebutton, ImageView, ProgressBar, textview, viewflipper, listview, GridView, stackview AdapterViewFlipper、ViewSub。 Note: remoteviews also does not support subclasses of the above view.

Remoteviews provides a series of setXXX () methods to set properties for child views of widgets. Refer to the API documentation for details.

1.2 RemoteViewsService

Remoteviews service is a service that manages remoteviews. Generally, when the AppWidget contains collection views such as GridView, listview and stackview, you need to use remoteviews service for update and management. The general steps for remoteviews service to update the collection view are:

(01) set the remoteviewsservice corresponding to remoteviews through the setremoteadapter() method.

(02) then implement the remoteviews factory interface in remoteviews service. Then, set each sub item of the collection view in the remoteviewsfactory interface, such as each item in the listview.

1.3 RemoteViewsFactory

Through the introduction in remoteviews service, we know that remoteviews service specifically manages the collection view in layout through remoteviews factory, which is an internal interface in remoteviews service. Remoteviews factory provides a series of methods to manage each item in a collection view. For example:

RemoteViews getViewAt(int position)

Get the view of the position item in the "collection view" through getviewat(). The view is returned as the object of remoteviews.

int getCount()

Get the total number of all children in the collection view through getcount().

The beauty of AppWidget -- AppWidget provider

We say that a female colleague is beautiful. In addition to the beautiful clothes and makeup she wears, I think the main reason is that she is beautiful. Similarly, the widget has the ability to attach to the desktop and update the view across processes mainly because the AppWidget provider is a broadcast receiver.

We found that in the above example, in the code automatically generated by as, in addition to the onupdate () method being rewritten by us, we also rewritten the onenable () and ondisable () methods, but they are empty implementations. When will these two methods be called? In addition, we say that the custom myappwidgetprovider inherits from the appwidgetprovider, which is a subclass of broadcastreceiver, but we don't override the onreceiver () method as we do for regular broadcast receivers? Let's follow the source code of AppWidget provider to find out.

There is not much code for this class. In fact, the appwidgetprovider has only the following methods in addition to constructing methods:

Onenable(): this method is called back when the widget is added to the desktop for the first time. It can be added multiple times, but it is only called for the first time. The action for broadcasting is action_ APPWIDGET_ ENABLE。

Onupdate(): this method will be called once when the widget is added or each time the widget is updated. The update cycle of the widget is configured in the configuration file, updateperiodmillis, which will be called every time it is updated. The corresponding broadcast action is: action_ APPWIDGET_ Update and action_ APPWIDGET_ RESTORED 。

Ondisabled(): called when the last widget of this type is removed from the desktop, and the action of the corresponding broadcast is action_ APPWIDGET_ DISABLED。

Ondeleted(): called every time a widget is deleted. The corresponding broadcast action is: action_ APPWIDGET_ DELETED 。

Onrestored(): called when the widget is restored from backup or the settings are restored. It is rarely used. The action corresponding to the broadcast is action_ APPWIDGET_ RESTORED。

Onappwidget optionschanged(): called when the widget layout changes. The action corresponding to the broadcast is action_ APPWIDGET_ OPTIONS_ CHANGED。

Finally, there is the onReceive () method, which is rewritten by the AppWidget provider to distribute specific time to the above method. Look at the source code:

AppWidget exercise

Next, write an example to learn other knowledge points in remoteviews. Buttons and listview are used in the layout of small parts in this example. Upper Code:

Layout file mul for widget_ app_ widget_ The provider.xml is as follows:

Configuration information for the widget mul_ app_ widget_ provider_ Info.xml is as follows:

Mulappwidgetprovider.java:

MainActivity.java:

The following focuses on the use of listview in widgets:

Finally, look at the manifest file:

After this widget is added to the desktop, there is an ImageView to display the robot, a button below and a listview on the right.

Here, we mainly look at how button and listview are used in remoteviews

The button setting text is the same as textview, because the button itself inherits from textview. The button setting click events are as follows:

The method setonclickpendingintent is used. Pendingintent represents the delayed intent, which is the same as that in the notification. Click here to jump to mainactivity.

The usage of listview is more complicated. First, you need to customize a class that inherits from remoteviewsservices, override the ongetviewfactory method, and return the object of the remoteviewsservice.remoteviewsfactory interface. Here, an internal class is defined to implement the interface, and multiple methods need to be rewritten, which is very similar to the multi layout adaptation of listview. The key method is

This method specifies the layout and content of each item in the listview, and sets the click event for the item through setonclickfillinintent() or setonclickpendingintent(). Here, I click item to replace the image of ImageView on the left. Override the onreceiver method of the mulappwidgetprovider class to handle the logic of replacing pictures.

The running effect of the program is as follows:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>