Android widget development Template

Source: Internet
Author: User

Widgets on Android use Java language development, which is much more efficient than W3C Widgets. you can do more to call system APIs, except for the restrictions on the UI, we can consider helping the system improve some appwidgets. android123 provides a template for developing widgets.

public class cwjwidget extends appwidgetprovider {
@ override
Public void onupdate (context, appwidgetmanager,
int [] appwidgetids) {
context. startservice (new intent (context, UpdateService. class); // create a service to prevent the wait timeout dialog box
}

Public static class UpdateService extends Service {// This internal service we recommend that you open a new thread to perform operations that are congested, such as network downloads.
@ Override
Public void onstart (intent, int startid ){

Remoteviews updateviews = buildupdate (this );

componentname thiswidget = new componentname (this, cwjwidget. class);
appwidgetmanager manager = appwidgetmanager. getinstance (this);
manager. updateappwidget (thiswidget, updateviews);
}

Public remoteviews buildupdate (context ){
Resources res = context. getresources ();
Remoteviews updateviews = new remoteviews (
Context. getpackagename (), R. layout. Main); // layout of the main widget

Pendingintent = pendingintent. getactivity (context,
0/* No requestcode */,
New intent (Android. provider. settings. action_device_info_settings ),
0/* no flags */);
Updateviews. setonclickpendingintent (R. Id. UI, pendingintent); // click View to open intent. The destination is the system information, which is the above action position.

updateviews. settextviewtext (r.id.info,
android. OS. build. version. codename + "" +
android. OS. build. ID); // here is the method for obtaining the system version of the API

updateviews. settextviewtext (R. id. changelist,
android. OS. build. fingerprint
);
return updateviews;
}

@ Override
Public ibinder onbind (intent ){
Return NULL;
}
}
}

Related androidmanifest. XML content

<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. android123.widget"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">

<Uses-SDK Android: minsdkversion = "3"/>
<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Cycler Android: Name = ". buildwidget" Android: Label = "android123_cwj">
<Intent-filter>
<Action Android: Name = "android. appwidget. Action. appwidget_update"/>
</Intent-filter>
<Meta-data Android: Name = "android. appwidget. provider" Android: Resource = "@ XML/widget"/>
</Cycler>
<Service android: Name = ". cwjwidget $ UpdateService"/>
</Application>

</Manifest>

Androidmanifest. xml the \ res \ XML \ widget. xml file mentioned above

<Appwidget-provider xmlns: Android = "http://schemas.android.com/apk/res/android" Android: minwidth = "150dip" Android: minheight = "72dip" Android: updateperiodmillis = "0" Android: initiallayout = "@ layout/widget"/>

main. the XML content is


Android: Id = "@ + ID/UI"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: Orientation = "vertical"
Android: padding = "6dip"

Android: Id =" @ + ID/INFO "
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "Left"
Android: textsize = "18dip"
/>

Android: Id =" @ + ID/changelist "
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "Left"
Android: layout_margintop = "4dip"
Android: textsize = "9dip"
/>

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.