Introduction to AppWidget control of Android UI component

Widget introduction

We can understand a widget as a small component (pendant) placed on the desktop. With a widget, we can easily perform various operations directly on the desktop, such as playing music.

When we long press the desktop, we can see the widget option, as shown in the following figure:

Click the widgets icon at the arrow in the figure above, and the following interface will appear: (all widgets)

Long press and hold any widget in the figure above to put it on the desktop.

The use of widget the implementation idea of widget (1) declare AppWidget in androidmanifest; (2) Define the configuration file of AppWidget in the XML directory; (3) Define the layout file of the widget in the layout directory; (4) Create a new class, inherit the AppWidget provider class, and implement the specific widget business logic.

We need to create a new class to inherit appwidgetprovider. Click the AppWidget provider and find that the AppWidget provider inherits from the broadcastreceiver.

Why is a widget a broadcast receiver? As we know, there is an onReceive method in the broadcastreceiver class to receive broadcasts. When we operate on the desktop pendant, it will inevitably lead to changes in the application, which involves the communication between the pendant and the application. At this time, it is best to use broadcasting to communicate.

Specific usage steps of widget

(1) Create a new class testwidget Java, inheriting AppWidget provider: testwidget java:

(2) Because the widget is a broadcast receiver, we need to register in the manifest file:

Line 04: action is the filter condition, which is used to filter the behavior and monitor the update of the widget. Line 08: Android: resource specifies the configuration of the widget. As we know, attributes are used to store data in the manifest file.

(3) Create a new file widget in the layout folder_ setting. XML: (widget's configuration file) setting_ widget. xml:

Line 08: Android: initiallayout specifies the layout of the widget. Line 09: Android: updateperiodmillis specifies the update time period. Line 10: Android: widgetcategory = "home_screen" displays the widget on the home screen (or on the lock screen)

(4) Create a new file layout in the layout folder_ widget. XML: (layout of widget) layout_ widget. xml:

At this point, the program can run. After running the program, long press the desktop and click the "widget" button to see the widget we just designed:

Long press the arrow in the figure above to drag and drop the widget we designed onto the desktop:

Widget click and update [important]

We know, testwidget Java inherits from appwidgetprovider. After inheriting broadcastreceiver, appwidgetprovider rewrites onReceive method, and then customizes many methods:

The above figure includes various methods such as deleting, disabling, enabling and updating. Of particular importance are the onReceive () method and the onupdate () method. When the widget is changed (such as installed on the desktop), the system will send an updated broadcast (shown in the red box above). We're setting_ widget. The update frequency of the widget is set in the XML, which will also call the update.

Someone may ask, why don't I update the weather and other widgets after I turn it on? This is because the process is killed, so we can only remove this control first and then install it. At this time, the application will send an update broadcast.

When we need to click and update the widget, we need to override the onupdate () method to send the broadcast. When the program is initialized, the system will call the onupdate () method.

The code in the onupdate() method is as follows:

Code explanation: first, you need to new a remoteviews. In the construction method, you need to pass two parameters, one is the package name (context. Getpackgename) and the other is the layout_widget. Then through remoteviews Setonclickpendingintent() sets the click event of the button. Setonclickpendingintent() needs to pass two parameters: one is ID (such as the button to be clicked) and the other is pendingintent. Pendingintent is the intention of the future. So we need to construct a pendingintent in advance, which needs to be passed through pendingintent Getbroadcast(). The getbroadcast () method needs to pass four parameters, one of which is intent. So we need to construct an intent. Send broadcast in intent and set action. After clicking the button, remember to call appwidgetmanager Update the updateappwidget (int [] appwidgetids, remoteviews views) method. The first parameter is the parameter in the onupdate method, which represents all controls.

After sending the broadcast of the button click time through intent in the onupdate () method, we need to receive the broadcast in the onReceive () method. The code in onreceive() method is as follows:

Code explanation: when the action of intent matches successfully, start to execute the settext after the click time. However, it is necessary to re create a remoteviews instead of sharing the remoteviews in the onupdate () method (this is a big pit). After executing the settext after the click event, remember to call appwidgetmanager For the updateappwidget (componentname, remoteviews) method, the first parameter is the component name. We need to update it ourselves. The second parameter is easy to explain.

Overall, testwidget The full version of Java code is as follows: testwidget java:

After running, drag the widget to the desktop. The effect is as follows:

After clicking the button, the effect is as follows:

Project files: (Android studio 2.1)

Of course, widgets have many other uses. For example: • communicate with service • interaction method of widget control. • How to make a desktop player widget

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
分享
二维码
< <上一篇
下一篇>>