首页>
知识库>
详情

Android Widget开发详解

2020-07-28 来源:CloudBest 阅读量: 0
关键词:

    本文和大家重点学习一下Widget开发的概念,本例是为了实现一个手机Android平台的Widget开发,该Widget中的内容是根据输入账号从叽歪网站上获得得。当然,这个过程需要叽歪的API,得到信息后进行处理并显示出来。大体流程就是这样。好了,进入第一步。
    Android Widget开发系列(二)
    该叽歪账号是测试账号,用户名是"students",密码是"111111"请不要擅自更改。
    2.建立一个Widget
    Androidreference中有关于如何建立一个Widget的详细方法,这里简要说明一下,详情可以查看AndroidSDK中自带的reference。
    要建立一个Widget开发程序,分为如下几个步骤:
    (1)创建一个类,让其继承类AppWidgetProvider,在AppWidgetProvider中有许多方法,例如onDelete(Context,int[]),onEnable(Context)等,但一般情况下我们只是覆写onUpdate(Context,AppWidgetManager,int[])方法。在该方法中,我们启动后台服务的类,一般是启动Thread类或者Android中的Service类。在该类中我们进行从服务器端获得数据并进行处理并在Widget中显示。
    (2)在你的AndroidMenifest.xml中添加一个receiver标签,让其指向你的AppWidgetProvider子类。内容如下:
    1. <receiverandroid:namereceiverandroid:name="JiwaiWidget"
    2. android:label="@string/app_name"
    3. android:icon="@drawable/jiwai">
    4. <intent-filter>
    5. <actionandroid:nameactionandroid:name="
    android.appwidget.action.APPWIDGET_UPDATE"/>
    6. </intent-filter>
    7. <meta-dataandroid:namemeta-dataandroid:name="android.appwidget.provider"
    8. android:resource="@xml/info"/>
    9. </receiver>
    对上面的代码进行解释:
    第一行指定该Widget开发的接收者是JiwaiWidget,即你建立的AppWidgetProvider子类;
    第二行指定该Widget的标签名称,值为value目录下string.xml中的app_name值;
    第三行指定该Widget开发的图标,值为drawable目录下jiwai图片;
    第四行-第六行是采用Android文档中提供的;
    第七行指定该Widget的描述者信息,该描述着中定义了Widget的相关信息,如该Widget的宽度、长度、自动更新的间隔时间等信息,该描述位于xml目录下的info.xml中。
    (3)编写你的Widget的provider文件信息(本例中是xml/info.xml)
    <appwidget-providerxmlns:androidappwidget-providerxmlns:android=
    "http://schemas.android.com/apk/res/android"
    android:minWidth="200dp"
    android:minHeight="90dp"
    android:updatePeriodMillis="43200000"
    android:initialLayout="@layout/appwidget"
    android:configure="com.lawrenst.jiwai.JiwaiConfigure">
    </appwidget-provider>
    其中android:updatePeriodMillis是自动更新的时间间隔,android:initialLayout是Widget的界面描述文件。Android:configure是可选的,如果你的Widget需要在启动时先启动一个Activity,则需要设定该项为你的Activity。本例中,需要你的嘀咕帐号和密码,所以应先显示一个Activity,输入你的账号和密码,然后将得到的信息在你的Widget中显示。
    (4)在layout目录下编写appwidget.xml文件,配置你的Widget的界面信息:
    xmlversionxmlversion="1.0"encoding="UTF-8"?>
    LinearLayoutxmlns:androidLinearLayoutxmlns:android=
    "http://schemas.android.om/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/widget"
    android:background="@drawable/title_a">
    LinearLayoutandroid:layout_widthLinearLayoutandroid:layout_width="fill_paren"
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:background="@drawable/title">
    <TextViewandroid:idTextViewandroid:id="@+id/username_display"
    android:textStyle="bold"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#ffffff"
    android:textSize="15px"
    android:gravity="left|center_vertical"
    android:paddingLeft="6px"/>
    </LinearLayout>
    <LinearLayoutandroid:orientationLinearLayoutandroid:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">