[Android Development] Getting Started with App Widget development Guide

Source: Internet
Author: User

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
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <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" >  
  3. <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 ">
  4. </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
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <!--appwidget-provider Widget configuration file--
  3. <!--android:minwidth min. width--
  4. <!--android:minheight Minimum height--
  5. <!--android:updateperiodmillis Component Update frequency (milliseconds)--
  6. <!--android:initiallayout Component layout XML Location--
  7. <!--android:configure Widget settings with activity--
  8. <appwidget -provider= "" xmlns:android= "http://schemas.android.com/apk/res/android" android:initiallayout= "@layout/widget" android:updateperiodmillis="86400000" android:minheight= "72DP" android:minwidth="294DP">
  9. </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
  1. Package basic.android.lesson35;
  2. Import Android.appwidget.AppWidgetManager;
  3. Import Android.appwidget.AppWidgetProvider;
  4. Import Android.content.Context;
  5. Import android.content.Intent;
  6. Import Android.util.Log;
  7. 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.
  8. Public class Hellowidgetprovider extends Appwidgetprovider {
  9. //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.
  10. @Override
  11. public void OnReceive (context context, Intent Intent) {
  12. LOG.I ("Yao", "Hellowidgetprovider-to-onreceive");
  13. super.onreceive (context, intent);
  14. }
  15. //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
  16. @Override
  17. public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager, int[] appwidgetids) {
  18. //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
  19. LOG.I ("Yao", "Hellowidgetprovider-to-onUpdate");
  20. super.onupdate (context, Appwidgetmanager, appwidgetids);
  21. }
  22. //When an app widget is removed from the desktop
  23. @Override
  24. public void ondeleted (context context, int[] appwidgetids) {
  25. LOG.I ("Yao", "Hellowidgetprovider-to-ondeleted");
  26. super.ondeleted (context, appwidgetids);
  27. }
  28. 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)
  29. @Override
  30. public void onenabled (context context) {
  31. LOG.I ("Yao", "Hellowidgetprovider-to-onenabled");
  32. super.onenabled (context);
  33. }
  34. This method is called when the last instance of the app widget is removed from the desktop.
  35. @Override
  36. public void ondisabled (context context) {
  37. LOG.I ("Yao", "Hellowidgetprovider-to-ondisabled");
  38. super.ondisabled (context);
  39. }
  40. }

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
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionname= "1.0" android:versioncode="1" package="basic.android.lesson35">
  3. <uses -sdk= "" android:minsdkversion="7">
  4. <application android:icon="@drawable/icon" android:label="@string/app_name">
  5. The android:name of <!--receiver points to the request processor of the widget or to the request recipient ,
  6. <receiver android:label="Hello,app Widget" android:name=". Hellowidgetprovider ">
  7. <intent -filter="">
  8. <!--widget Default event action--
  9. <action android:name="Android.appwidget.action.APPWIDGET_UPDATE"></ Action>
  10. </Intent>
  11. <!--widget metadata, name is write-dead, resource refers to the widget's configuration file --
  12. <meta -data= " android:name=" Android.appwidget.provider " android:resource=" @ Xml/provider_info ">
  13. </receiver>
  14. </Application>
  15. </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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.