The main topics in this section include the Android Desktop widget, the app Widget Development Guide, and a simple example of how to visually explain the app widget.
The concept of widgets, app widgets, Web apps
The original concept of the widget was a 98 Apple engineer named Rose, who was officially known until 2003, but then many large companies began to accept and apply the idea. Now we see the widgets in the Apple system that press F4 pop-up dashboard, the nifty gadgets on the side bar of Windows 7 called Gadget (widget variants), and other widgets such as Yahoo Widgets. They have a common feature is the use of foreground web development technology (such as HTML, CSS, Javascript) to make gadgets, widgets.
In the Android system, almost every visual view component is called a widget, and the name may have been a fad.
App widgets are the stuff that comes from Android 1.5, which is an app widget that you can put on your Android desktop. At this point he functions much like the Windows Sidebar gadget, but unfortunately his adoption is not a technology such as HTML. Of course, the app widget is our main character, originally he should be called the Widget, at least also called Gadget Bar, unfortunately this name has been his own system occupied, so have to change the name of the app widget.
Finally, the Web App, or Android Web application, may be called Mobile Web application more accurately. We found that there are a lot of intelligent system platform, such as iOS, Android, Windows Phone, WebOS, BlackBerry and so on, they adopt a different technical framework, there is no way to write a program can be run on each system? The answer is yes, write an app based on the WebKit browser. We use HTML5, CSS3, JavaScript, WebKit and other technologies to write the Web application may be a big trend in the future is not sure AH. We have the opportunity to talk about the development of Android WEB application.
Second, a simple example of the app widget: Hello app widget
The technical implementation of the APP widget is a little bit around, so let's use one of the simplest examples of Hello app widgets to work through it and then explain it to this example, and maybe you'll understand faster.
1, create a new project Lesson35_helloappwidget, note that creation Activity may not be selected when creating.
2, ready a widget display layout file Layout/widget.xml, the content is as follows:
xml/html Code
- <? XML version= "1.0" encoding="Utf-8"?>
- <linearlayout android:layout_ Height= "fill_parent" android:layout_width= "fill_parent" android:orientation= "vertical" xmlns:android= "http://schemas.android.com/apk/res/android" Android:gravity= "center" >
- <textview android:layout_height="wrap_content" android:layout_width="Wrap_content " Android:id= "@+id/textview1" android:text= "Welcome to the world of app widgets!" " android:textcolor="#ff0000ff ">
- </TextView></linearlayout>
3, ready a widget profile xml/provider_info.xml, which configures the widget can occupy the screen length and width, update frequency, the displayed layout file (that is, the above layout file), and its contents are as follows:
xml/html Code
- <? XML version= "1.0" encoding="Utf-8"?>
- <!--appwidget-provider Widget configuration file--
- <!--android:minwidth min. width--
- <!--android:minheight Minimum height--
- <!--android:updateperiodmillis Component Update frequency (milliseconds)--
- <!--android:initiallayout Component layout XML Location--
- <!--android:configure Widget settings with activity--
- <appwidget -provider= "" xmlns:android= "http://schemas.android.com/apk/res/android" android:initiallayout= "@layout/widget" android:updateperiodmillis="86400000" android:minheight= "72DP" android:minwidth="294DP">
- </appwidget>
4. Prepare a Java file to process the widget request, Basic.android.lesson35 under the package of Hellowidgetprovider, he inherited the Appwidgetprovider class, in this case there is no request to deal with the specific code, I wrote a lot of comments in the Java file, easy to understand. The contents are as follows:
Java code
- Package basic.android.lesson35;
- Import Android.appwidget.AppWidgetManager;
- Import Android.appwidget.AppWidgetProvider;
- Import Android.content.Context;
- Import android.content.Intent;
- Import Android.util.Log;
- Appwidgetprovider is a subclass of Broadcastreceiver, which is essentially a broadcast receiver, which is designed to receive various requests from Widget components (delivered with intent), so if I were to give him a name. I'll name him appwidgetreceiver, and every widget will have a appwidgetprovider.
- Public class Hellowidgetprovider extends Appwidgetprovider {
- //Each request is passed to the OnReceive method, which determines whether to handle it or distribute it to the following four special methods based on the action type in the intent parameter.
- @Override
- public void OnReceive (context context, Intent Intent) {
- LOG.I ("Yao", "Hellowidgetprovider-to-onreceive");
- super.onreceive (context, intent);
- }
- //If the widget auto-update time is up, or if other events will cause the widget to change, or if the value of intent is Android.appwidget.action.APPWIDGET_UPDATE, Then the onupdate is called, and the following three methods are similar
- @Override
- public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager, int[] appwidgetids) {
- //appwidgetmanager As the name implies is the Appwidget manager, Appwidgetids all the widgets on the desktop will be assigned a unique ID identifier, then this array is their list
- LOG.I ("Yao", "Hellowidgetprovider-to-onUpdate");
- super.onupdate (context, Appwidgetmanager, appwidgetids);
- }
- //When an app widget is removed from the desktop
- @Override
- public void ondeleted (context context, int[] appwidgetids) {
- LOG.I ("Yao", "Hellowidgetprovider-to-ondeleted");
- super.ondeleted (context, appwidgetids);
- }
- call when the app widget is first placed on the desktop (the same app widget can be placed on the desktop multiple times, so that's what it says)
- @Override
- public void onenabled (context context) {
- LOG.I ("Yao", "Hellowidgetprovider-to-onenabled");
- super.onenabled (context);
- }
- This method is called when the last instance of the app widget is removed from the desktop.
- @Override
- public void ondisabled (context context) {
- LOG.I ("Yao", "Hellowidgetprovider-to-ondisabled");
- super.ondisabled (context);
- }
- }
5, configure the Androidmanifest.xml file, add a receiver tag, this label looks like the previous Broadreceiver configuration, the specific content is as follows:
xml/html Code
- <? XML version= "1.0" encoding="Utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionname= "1.0" android:versioncode="1" package="basic.android.lesson35">
- <uses -sdk= "" android:minsdkversion="7">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- The android:name of <!--receiver points to the request processor of the widget or to the request recipient ,
- <receiver android:label="Hello,app Widget" android:name=". Hellowidgetprovider ">
- <intent -filter="">
- <!--widget Default event action--
- <action android:name="Android.appwidget.action.APPWIDGET_UPDATE"></ Action>
- </Intent>
- <!--widget metadata, name is write-dead, resource refers to the widget's configuration file --
- <meta -data= " android:name=" Android.appwidget.provider " android:resource=" @ Xml/provider_info ">
- </receiver>
- </Application>
- </uses></manifest>
6, compile and run the program, we know that this widget program, even if it is finished and will not appear in the program list, because it does not have main Activity, below I give unclear classmate how to put a widget on the desktop.
Press and hold on the desktop on the emulator and wait for the following dialog to pop up:
Select Widgets:
Select the Hello,app Widget: