Android Appwidget Development Combat

Source: Internet
Author: User

Appwidget Framework class1.AppWidgetProvider

Inherit from Broadcastrecevier and receive notifications when Appwidget applies update, enable, disable, and delete. Of these, onUpdate and onreceive are the most commonly used methods, and they receive update notifications.
The inherited self-appwidgetprovider can be implemented in the following ways:
Ondeleted (context context, int[] appwidgetids)
Ondisabled (Context context)
Onenabled (Context context)
OnReceive (context context, Intent Intent)
OnUpdate (context context, Appwidgetmanager Appwidgetmanager, int[] appwidgetids)

2.AppWidgetProvderInfo

Describes the size of the appwidget, the update frequency, and the initial interface, in the form of an XML file in the application's res/xml/directory.

3.AppWidgetManger

Responsible for managing Appwidget and sending notifications to Appwidgetprovider.
bindappwidgetid (int appwidgetid, componentname provider): Binding Appwidgetid by a given componentname
Getappwidgetids (componentname provider): Get Appwidgetid with a given componentname
getappwidgetinfo (int appwidgetid): Get appwidget information via Appwidgetid
getinstalledproviders (): Returns information for a list<appwidgetproviderinfo>
getinstance (Context context): Gets the context object used by the Appwidgetmanger instance
updateappwidget (int[] appwidgetids, remoteviews views): Modify the incoming RemoteView by Appwidgetid and re-refresh the Appwidget component
Updateappwidget (componentname provider, remoteviews views): Modify the incoming Remoeteview by componentname and re-refresh the Appwidget component
updateappwidget (int appwidgetid, remoteviews views): Modify the incoming RemoteView by Appwidgetid and re-refresh the Appwidget component

4.RemoteViews

A class that can be run in another app process, sending a notification to Appwidgetprovider.

Demo

A simple Appwidget step

1. Create a new XML folder under Res, and then create a new Appwidget_provider.xml

<?xml version= "1.0" encoding= "UTF-8"? ><appwidget-provider xmlns:android= "http://schemas.android.com/apk/ Res/android "android:minwidth=" 60DP "android:minheight=" 30DP "android:updateperiodmillis=" 86400000 "    android: initiallayout= "@layout/widget_main" ></appwidget-provider>
2. Create a new layout file under the Layouts folder Widget_main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:id=" @+id/layout "    android:layout_width=" fill_parent "    android:layout_height=" Fill_ Parent "    android:orientation=" vertical "><textview        android:id=" @+id/tv "        android:layout_width=" Wrap_content "        android:layout_height=" wrap_content "        android:clickable=" true "        android:text=" dot dot Me " ></TextView></LinearLayout>
3. Define class inherits from Appwidgetprovider
public class Myappwidgetprovider extends Appwidgetprovider {private remoteviews remoteviews;        @Override public void OnReceive (context context, Intent Intent) {super.onreceive (context, Intent);        if (remoteviews = = null) {remoteviews = new Remoteviews (Context.getpackagename (), r.layout.widget_main);                } if (Intent.getaction (). Equals ("Com.example.widgettest")) {if (Commonvaliable.ischange) {            Remoteviews.settextviewtext (r.id.tv, "Dot dot Me");            } else {Remoteviews.settextviewtext (r.id.tv, "Buy and Buy Me");            } toast.maketext (Context, boolean.tostring (Commonvaliable.ischange), Toast.length_long). Show (); Commonvaliable.ischange =!        Commonvaliable.ischange;        } Appwidgetmanager Appwidgetmanger = appwidgetmanager.getinstance (context);        int[] Appids = appwidgetmanger.getappwidgetids (new componentname (context, myappwidgetprovider.class)); Appwidgetmanger.updAteappwidget (Appids, remoteviews); } @Override public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager, int[] appwidgetids) {fi        nal int N = appwidgetids.length;            for (int i = 0; i < N; i++) {int appwidgetid = appwidgetids[i];        Updateappwidget (context, Appwidgetmanager, Appwidgetid); }} public void Updateappwidget (context context, Appwidgetmanager Appwidgemanger, int appwidgetid) {Remotevi        EWS = new Remoteviews (Context.getpackagename (), r.layout.widget_main);        Intent Intent = new Intent ("Com.example.widgettest");        Pendingintent pendingintent = pendingintent.getbroadcast (context, 0, intent, 0);        Remoteviews.setonclickpendingintent (r.id.tv, pendingintent);    Appwidgemanger.updateappwidget (Appwidgetid, remoteviews); }}
4. Declare the following under the application node of the manifest file

<receiver android:name= ". Myappwidgetprovider ">    <meta-data        android:name=" Android.appwidget.provider "        android:resource= "@xml/appwidget_provider" >    </meta-data>    <intent-filter>        <action android:name= " Com.example.widgettest "/>        <action android:name=" Android.appwidget.action.APPWIDGET_UPDATE "/>    </intent-filter></receiver>

Download Demo please poke


Android Appwidget Development Combat

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.