Android Widget開發詳解

來源:互聯網
上載者:User

本文和大家重點學習一下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"
 android:layout_height="wrap_content"/>
 
 </LinearLayout>
 
 <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/code"
 android:textColor="#ff8cff"
 android:capitalize="characters"/>
 
 <EditTextandroid:idEditTextandroid:id="@+id/password"
 android:layout_width="200px"
 android:layout_height="wrap_content"
 android:password="true"/>
 </LinearLayout>
 
 <LinearLayoutandroid:orientationLinearLayoutandroid:orientation="horizontal"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:gravity="center_horizontal">
 
 <Button
 android:id="@+id/submit"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Submit"
 />
 </LinearLayout>
 </LinearLayout>
  有兩個EditText用來輸入使用者名稱和密碼,另外還有一個Button對象。
  準備工作差不多了,下面就可以寫代碼了。

 

本文出自“知識海洋”
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.