自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android Widget開發(fā)詳解

移動(dòng)開發(fā)
Android Widget開發(fā)詳解是本文要介紹的內(nèi)容,本例是為了實(shí)現(xiàn)一個(gè)手機(jī)Android平臺(tái)的Widget開發(fā),該Widget中的內(nèi)容是根據(jù)輸入賬號(hào)從嘰歪網(wǎng)站上獲得得。

Android Widget開發(fā)詳解是本文要介紹的內(nèi)容,主要是來了解并學(xué)習(xí)Widget開發(fā)的概念,本例是為了實(shí)現(xiàn)一個(gè)手機(jī)Android平臺(tái)的Widget開發(fā),該Widget中的內(nèi)容是根據(jù)輸入賬號(hào)從嘰歪網(wǎng)站上獲得得。當(dāng)然,這個(gè)過程需要嘰歪的API,得到信息后進(jìn)行處理并顯示出來。大體流程就是這樣。好了,進(jìn)入***步。

1、該嘰歪賬號(hào)是測(cè)試賬號(hào),用戶名是“students”,密碼是“111111”請(qǐng)不要擅自更改。

2、建立一個(gè)Widget

Androidreference中有關(guān)于如何建立一個(gè)Widget的詳細(xì)方法,這里簡(jiǎn)要說明一下,詳情可以查看AndroidSDK中自帶的reference。

要建立一個(gè)Widget開發(fā)程序,分為如下幾個(gè)步驟:

(1)創(chuàng)建一個(gè)類,讓其繼承類AppWidgetProvider,在AppWidgetProvider中有許多方法,例如onDelete(Context,int[]),onEnable(Context)等,但一般情況下我們只是覆寫onUpdate(Context,AppWidgetManager,int[])方法。在該方法中,我們啟動(dòng)后臺(tái)服務(wù)的類,一般是啟動(dòng)Thread類或者Android中的Service類。在該類中我們進(jìn)行從服務(wù)器端獲得數(shù)據(jù)并進(jìn)行處理并在Widget中顯示。

(2)在你的AndroidMenifest.xml中添加一個(gè)receiver標(biāo)簽,讓其指向你的AppWidgetProvider子類。內(nèi)容如下:

  1. <receiver android:name="JiwaiWidget"     
  2.     android:label="@string/app_name"     
  3.     android:icon="@drawable/jiwai">     
  4.     <intent-filter>     
  5.     <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>     
  6.     </intent-filter>     
  7.     <meta-data android:name="android.appwidget.provider"     
  8.     android:resource="@xml/info"/>     
  9.     </receiver>     
  10. <receiver android:name="JiwaiWidget"   
  11.     android:label="@string/app_name"   
  12.     android:icon="@drawable/jiwai">   
  13.     <intent-filter>   
  14.     <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>   
  15.     </intent-filter>   
  16.     <meta-data android:name="android.appwidget.provider"   
  17.     android:resource="@xml/info"/>   
  18.     </receiver>  

對(duì)上面的代碼進(jìn)行解釋:

***行指定該Widget開發(fā)的接收者是JiwaiWidget,即你建立的AppWidgetProvider子類;

第二行指定該Widget的標(biāo)簽名稱,值為value目錄下string.xml中的app_name值;

第三行指定該Widget開發(fā)的圖標(biāo),值為drawable目錄下jiwai圖片;

第四行-第六行是采用Android文檔中提供的;

第七行指定該Widget的描述者信息,該描述著中定義了Widget的相關(guān)信息,如該Widget的寬度、長(zhǎng)度、自動(dòng)更新的間隔時(shí)間等信息,該描述位于xml目錄下的info.xml中。

(3)編寫你的Widget的provider文件信息(本例中是xml/info.xml)

  1. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"     
  2.     android:minWidth="200dp"     
  3.     android:minHeight="90dp"     
  4.     android:updatePeriodMillis="43200000"     
  5.     android:initialLayout="@layout/appwidget"     
  6.     android:configure="com.lawrenst.jiwai.JiwaiConfigure">     
  7.     </appwidget-provider>     
  8. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"   
  9.     android:minWidth="200dp"   
  10.     android:minHeight="90dp"   
  11.     android:updatePeriodMillis="43200000"   
  12.     android:initialLayout="@layout/appwidget"   
  13.     android:configure="com.lawrenst.jiwai.JiwaiConfigure">   
  14.     </appwidget-provider>  

其中android:updatePeriodMillis是自動(dòng)更新的時(shí)間間隔,android:initialLayout是Widget的界面描述文件。Android:configure是可選的,如果你的Widget需要在啟動(dòng)時(shí)先啟動(dòng)一個(gè)Activity,則需要設(shè)定該項(xiàng)為你的Activity。本例中,需要你的嘀咕帳號(hào)和密碼,所以應(yīng)先顯示一個(gè)Activity,輸入你的賬號(hào)和密碼,然后將得到的信息在你的Widget中顯示。

(4)在layout目錄下編寫appwidget.xml文件,配置你的Widget的界面信息:

  1. <?xml version="1.0" encoding="UTF-8"?>     
  2.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
  3.     android:layout_width="fill_parent"     
  4.     android:layout_height="wrap_content"     
  5.     android:orientation="vertical"     
  6.     android:id="@+id/widget"     
  7.     android:background="@drawable/title_a">     
  8.     <LinearLayout android:layout_width="fill_parent"     
  9.     android:orientation="horizontal"     
  10.     android:layout_height="wrap_content"     
  11.     android:background="@drawable/title">     
  12.     <TextView android:id="@+id/username_display"     
  13.     android:textStyle="bold"     
  14.     android:layout_width="wrap_content"     
  15.     android:layout_height="fill_parent"     
  16.     android:textColor="#ffffff"     
  17.     android:textSize="15px"     
  18.     android:gravity="left|center_vertical"     
  19.     android:paddingLeft="6px"/>     
  20.     </LinearLayout>     
  21.     <LinearLayout android:orientation="vertical"     
  22.     android:layout_width="fill_parent"     
  23.     android:layout_height="fill_parent">     
  24.          
  25.     <TextView android:id="@+id/text1"     
  26.     android:layout_width="fill_parent"     
  27.     android:textColor="#ffffff"     
  28.     android:textSize="12px"     
  29.     android:gravity="center_vertical|left"     
  30.     android:paddingLeft="6px"     
  31.     android:layout_height="30px">     
  32.     </TextView>     
  33.          
  34.     <TextView android:id="@+id/text2"     
  35.     android:textColor="#ffffff"     
  36.     android:layout_height="30px"     
  37.     android:gravity="center_vertical|left"     
  38.     android:textSize="12px"     
  39.     android:paddingLeft="6px"     
  40.     android:layout_width="fill_parent">     
  41.     </TextView>     
  42.     </LinearLayout>     
  43.     </LinearLayout>     
  44. <?xml version="1.0" encoding="UTF-8"?>   
  45.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  46.     android:layout_width="fill_parent"   
  47.     android:layout_height="wrap_content"   
  48.     android:orientation="vertical"   
  49.     android:id="@+id/widget"   
  50.     android:background="@drawable/title_a">   
  51.     <LinearLayout android:layout_width="fill_parent"   
  52.     android:orientation="horizontal"   
  53.     android:layout_height="wrap_content"   
  54.     android:background="@drawable/title">   
  55.     <TextView android:id="@+id/username_display"   
  56.     android:textStyle="bold"   
  57.     android:layout_width="wrap_content"   
  58.     android:layout_height="fill_parent"   
  59.     android:textColor="#ffffff"   
  60.     android:textSize="15px"   
  61.     android:gravity="left|center_vertical"   
  62.     android:paddingLeft="6px"/>   
  63.     </LinearLayout>   
  64.     <LinearLayout android:orientation="vertical"   
  65.     android:layout_width="fill_parent"   
  66.     android:layout_height="fill_parent">   
  67.        
  68.     <TextView android:id="@+id/text1"   
  69.     android:layout_width="fill_parent"   
  70.     android:textColor="#ffffff"   
  71.     android:textSize="12px"   
  72.     android:gravity="center_vertical|left"   
  73.     android:paddingLeft="6px"   
  74.     android:layout_height="30px">   
  75.     </TextView>   
  76.        
  77.     <TextView android:id="@+id/text2"   
  78.     android:textColor="#ffffff"   
  79.     android:layout_height="30px"   
  80.     android:gravity="center_vertical|left"   
  81.     android:textSize="12px"   
  82.     android:paddingLeft="6px"   
  83.     android:layout_width="fill_parent">   
  84.     </TextView>   
  85.     </LinearLayout>   
  86.     </LinearLayout>  

該Widget中包括三個(gè)Textview,兩個(gè)用來顯示嘰歪的信息,一個(gè)用來顯示用戶名,上述代碼比較簡(jiǎn)單,故不做解釋。

(5)由于需要一個(gè)Acvivity對(duì)象用來輸入賬戶信息,所以在layout目錄下新建一個(gè)login.xml,作為Activity的配置文件:

  1. <?xml version="1.0" encoding="utf-8"?>     
  2.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
  3.     android:orientation="vertical"     
  4.     android:layout_width="fill_parent"     
  5.     android:layout_height="fill_parent"     
  6.     >     
  7.     <TextView android:layout_width="fill_parent"     
  8.     android:layout_height="wrap_content"     
  9.     android:text="@string/hello"     
  10.     android:textColor="#ff8c00"     
  11.     android:capitalize="characters"     
  12.     android:textStyle="bold"/>     
  13.          
  14.     <LinearLayout android:orientation="horizontal"     
  15.     android:layout_width="fill_parent"     
  16.     android:layout_height="wrap_content"     
  17.     android:gravity="center_horizontal">     
  18.          
  19.     <TextView android:layout_width="wrap_content"     
  20.     android:layout_height="wrap_content"     
  21.     android:text="@string/user"     
  22.     android:textColor="#ff8cff"     
  23.     android:capitalize="characters"/>     
  24.          
  25.     <EditText android:id="@+id/username"     
  26.     android:layout_width="200px"     
  27.     android:layout_height="wrap_content"/>     
  28.          
  29.     </LinearLayout>     
  30.          
  31.     <LinearLayout android:orientation="horizontal"     
  32.     android:layout_width="fill_parent"     
  33.     android:layout_height="wrap_content"     
  34.     android:gravity="center_horizontal">     
  35.          
  36.     <TextView android:layout_width="wrap_content"     
  37.     android:layout_height="wrap_content"     
  38.     android:text="@string/code"     
  39.     android:textColor="#ff8cff"     
  40.     android:capitalize="characters"/>     
  41.          
  42.     <EditText android:id="@+id/password"     
  43.     android:layout_width="200px"     
  44.     android:layout_height="wrap_content"     
  45.     android:password="true"/>     
  46.     </LinearLayout>     
  47.          
  48.     <LinearLayout android:orientation="horizontal"     
  49.     android:layout_width="fill_parent"     
  50.     android:layout_height="wrap_content"     
  51.     android:gravity="center_horizontal">     
  52.          
  53.     <Button     
  54.     android:id="@+id/submit"     
  55.     android:layout_width="wrap_content"     
  56.     android:layout_height="wrap_content"     
  57.     android:text="Submit"     
  58.     />     
  59.     </LinearLayout>     
  60.     </LinearLayout>    
  61. <?xml version="1.0" encoding="utf-8"?>   
  62.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  63.     android:orientation="vertical"   
  64.     android:layout_width="fill_parent"   
  65.     android:layout_height="fill_parent"   
  66.     >   
  67.     <TextView android:layout_width="fill_parent"   
  68.     android:layout_height="wrap_content"   
  69.     android:text="@string/hello"   
  70.     android:textColor="#ff8c00"   
  71.     android:capitalize="characters"   
  72.     android:textStyle="bold"/>   
  73.        
  74.     <LinearLayout android:orientation="horizontal"   
  75.     android:layout_width="fill_parent"   
  76.     android:layout_height="wrap_content"   
  77.     android:gravity="center_horizontal">   
  78.        
  79.     <TextView android:layout_width="wrap_content"   
  80.     android:layout_height="wrap_content"   
  81.     android:text="@string/user"   
  82.     android:textColor="#ff8cff"   
  83.     android:capitalize="characters"/>   
  84.        
  85.     <EditText android:id="@+id/username"   
  86.     android:layout_width="200px"   
  87.     android:layout_height="wrap_content"/>   
  88.        
  89.     </LinearLayout>   
  90.        
  91.     <LinearLayout android:orientation="horizontal"   
  92.     android:layout_width="fill_parent"   
  93.     android:layout_height="wrap_content"   
  94.     android:gravity="center_horizontal">   
  95.        
  96.     <TextView android:layout_width="wrap_content"   
  97.     android:layout_height="wrap_content"   
  98.     android:text="@string/code"   
  99.     android:textColor="#ff8cff"   
  100.     android:capitalize="characters"/>   
  101.        
  102.     <EditText android:id="@+id/password"   
  103.     android:layout_width="200px"   
  104.     android:layout_height="wrap_content"   
  105.     android:password="true"/>   
  106.     </LinearLayout>   
  107.        
  108.     <LinearLayout android:orientation="horizontal"   
  109.     android:layout_width="fill_parent"   
  110.     android:layout_height="wrap_content"   
  111.     android:gravity="center_horizontal">   
  112.        
  113.     <Button   
  114.     android:id="@+id/submit"   
  115.     android:layout_width="wrap_content"   
  116.     android:layout_height="wrap_content"   
  117.     android:text="Submit"   
  118.     />   
  119.     </LinearLayout>   
  120.     </LinearLayout> 

有兩個(gè)EditText用來輸入用戶名和密碼,另外還有一個(gè)Button對(duì)象。

小結(jié):Android Widget開發(fā)詳解是本文要介紹的內(nèi)容,主要是來了解Android Widget內(nèi)容的學(xué)習(xí)能對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來源: CSDN博客
相關(guān)推薦

2010-07-13 09:02:19

Widget開發(fā)

2011-09-08 13:11:07

Android Wid實(shí)例

2011-09-07 13:18:40

Android Wid

2011-09-07 10:34:48

Android Wid

2011-02-28 13:04:27

RelativeLayAndroid Wid

2010-07-23 08:54:02

2011-09-09 10:00:20

Android Wid開發(fā)

2011-09-07 17:54:40

Android Wid開發(fā)

2011-09-07 14:39:47

Android Wid設(shè)計(jì)

2011-09-07 13:00:36

2011-09-07 11:15:25

2011-09-08 10:04:07

Windows MobWidget

2011-05-03 15:13:23

BlackBerryWidget

2011-09-07 14:20:42

Android Wid組件

2011-09-07 13:06:04

Android Wid

2010-05-13 10:45:38

2011-09-13 15:35:40

Widget

2011-09-07 16:28:46

QT WidgetQWidget

2010-07-13 09:08:27

Widget開發(fā)

2011-09-08 15:07:10

Android Wid搭建
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)