当前位置:操作系统 > 安卓/Android >>

Android Widget开发详解

本文和大家重点学习一下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子类。内容如下:
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->   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)
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> <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的界面信息:
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->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">
 
 <TextViewandroid:idTextViewandroid:id="@+id/text1"
 android:layout_width="fill_parent"
 android:textColor="#ffffff"
 android:textSize="12px"
 android:gravity="center_vertical|left"
 android:paddingLeft="6px"
 android:layout_height="30px">
 </TextView>
 
 <TextViewandroid:idTextViewandroid:id="@+id/text2"
 android:textColor="#ffffff"
 android:layout_height="30px"
 android:gravity="center_vertical|left"
 android:textSize="12px"
 android:paddingLeft="6px"
 android:layout_width="fill_parent">
 </TextView>
 </LinearLayout>
 </LinearLayout>
  该Widget中包括三个Textview,两个用来显示叽歪的信息,一个用来显示用户名,上述代码比较简单,故不做解释。
 
  (5)由于需要一个Acvivity对象用来输入账户信息,所以在layout目录下新建一个login.xml,作为Activity的配置文件:
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?xmlversionxmlversion="1.0"encoding="utf-8"?>
 LinearLayoutxmlns:androidLinearLayoutxmlns:android=
"http://schemas.android.om/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
 <TextViewandroid:layout_widthTextViewandroid:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/hello"
 android:textColor="#ff8c00"
 android:capitalize="characters"
 android:textStyle="bold"/>
 
 <LinearLayoutandroid:orientationLinearLayoutandroid:orientation="horizontal"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:gravity="center_horizontal">
 
 <TextViewandroid:layout_widthTextViewandroid:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/user"
 android:textColor="#ff8cff"
 android:capitalize="characters"/>
 
 <EditTextandroid:idEditTextandroid:id="@+id/username"
 android:layout_width="200px"
 andr

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,