Teach you how to make Android Widgets

Source: Internet
Author: User
Teach you how to make Android Widgets

Recently, we have to make a bar code scanner (barcode scan) widget in the project and put it on the desktop of the mobile phone. I checked some information on the Internet, investigated the information, and shared the methods with you.

First, let's talk about what widgets are. Widgets are desktop widgets. Android has four built-in clock, music player, photo frame, and Google search.
Widgets, but this does not prevent developers from developing more beautiful versions with richer functions. In addition, microblogs, RSS subscriptions, stock market information, and weather forecasts are also popular.
.

How can I add widgets to the desktop?

Press the cell phone desktop to display the dialog. Select widgets to enter the widget list of the mobile phone. The list shows all the widgets that can be used on the mobile phone, including those we made by ourselves (after this example is completed, you will find that the widgets we made appear here at night ), the widget appears on the desktop.

The following describes how to create a widget:

1. declare that the appwidgetprovider class is in the androidmanifest. xml file of the application.

<Cycler Android: Name = "com. demo_barcodewidget.barcodewidgetprovider"

Android: icon = "@ drawable/homenav_barcode"

Android: Label = "yellowbook barcode failed">

<Intent-filter>

<Action Android: Name = "android. appwidget. Action. appwidget_update"/>

</Intent-filter>

<Meta-data Android: Name = "android. appwidget. provider" Android: Resource = "@ XML/barcode_widget"/>

</Cycler>

  • The <intent-filter> element must contain the <action> element with the Android: Name attribute. This attribute specifies
    Appwietprovider can receive action_appwidget_update broadcasts. This is the only broadcast you must explicitly declare.
    Appwidgetmanager automatically sends a widget broadcast to appwidgetprovider.
  •  
    <Meta-data> indicates the appwidgetproviderinfo resource. The following attributes are required: Android: Name-indicates the number of elements.
    Data name. Use Android. appwidget. provider to specify the data as the description of appwidgetproviderinfo.

Android: resource-specifies the location of the appwidgetproviderinfo resource.

2.
Add the appwidgetproviderinfor metadata in RES/XML/widget_provider.xml:

<Appwidget-provider xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: minwidth = "47dip"

Android: minheight = "44dip"

Android: updateperiodmillis = "10000"

>

</Appwidget-provider>

  • This file is stored inIn Res/XML/folder, if res does not contain an XML folder, you need to create a new one.
  • Note: appwidgetproviderinfo
    Define basic widget attributes, such as the minimum layout dimension, initial layout resources, update the widget frequency, and configure the activity to be started at the creation time. Use a single
    The <appwidget-provider> element of defines the appwidgetproviderinfo object and stores it in RES/XML of the project.
    /Folder.


3.
Create a widget layout file in RES/layout:

<Imageview xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: src = "@ drawable/homenav_barcode" Android: Id = "@ + ID/barcode_widget_logo"

Android: layout_height = "wrap_content" Android: layout_width = "wrap_content"> </imageview>

  • This is the layout widget displayed on the desktop. In this example, there is only one imageview.
  • You
    You must define an initial layout file for your widget in XML and save it in the Res/layout/directory of the project. If you are familiar with declaring layout in XML, create
    Widget layout is simple. You can use the view object to design your widgets. However, you must note that the widget is based on remoteviews and does not support layout or
    View Controls.


4. Create the appwidgetprovider class

Package com. demo_barcodewidget;

Import Android. App. pendingintent;

Import Android. appwidget. appwidgetmanager;

Import Android. appwidget. appwidgetprovider;

Import Android. content. componentname;

Import Android. content. context;

Import Android. content. intent;

Import Android. widget. remoteviews;

Public class barcodewidgetprovider extends appwidgetprovider {

Public void onupdate (context, appwidgetmanager, int [] appwidgetids ){

Remoteviews mremoteviews = buildupdate (context );

Componentname thiswidget = new componentname (context, barcodewidgetprovider. Class );

Appwidgetmanager manager = appwidgetmanager. getinstance (context );

Manager. updateappwidget (thiswidget, mremoteviews );

}

Private remoteviews buildupdate (context ){

Remoteviews Rv = new remoteviews (context. getpackagename (), R. layout. barcode_scanner_widget );

Intent I = new intent (context, demo_barcodewidget.class );

I. addflags (intent. flag_activity_new_task );

RV. setonclickpendingintent (R. Id. barcode_widget_logo, pendingintent. getactivity (context, 0, I, 0 ));

Return RV;

}

}

  • The appwidgetprovider class inherited from broadcastreceiver is a class that facilitates processing widget broadcast.
    Appwidgetprovider only receives event broadcasts related to widgets, such as when widgets are updated, deleted, available, and unavailable. When these broadcast events occur
    Wait, appwidgetprovider receives the following method call
    Use: onupdated, ondeleted, onenabled, ondisabled, onreceive.
  • Most
    The important appwidgetprovider callback function is onupdated, because it is called when every widget is added (unless you use the configuration
    Activity ). If your widget receives any user interaction event, you need to register the event handler in the callback function. If your widget does not create a temporary file
    Or database, or execute the job that requires clean-up, onupdated may be the only callback function you need to register.
  • Used here
    Remoteview to implement the widget Event Response (onclick event) and display and refresh of the widget page (in fact, this example is very simple, but it only describes the creation of the widget.
    Step. The widget click event only jumps to the main
    Activity ). In fact, Widgets are rich in functionality: You can report news, buy things for you, list your favorite bands, and watch videos recently.
    In addition, it is a diligent Butler. You do not have to go to the Flickr or weather forecast website yourself. widgets will take the initiative to bring you information. Widgets can be displayed in multiple forms.
    Show, video, MAP, news, games ......

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.