[安卓開發]App Widget開發入門指導

來源:互聯網
上載者:User

標籤:android   http   java   color   使用   strong   

      本節所要講的主要內容包括Android案頭小組件、App Widget的開發入門指導,並通過一個簡單一實例的形式來直觀的講解App Widget。

       一、Widget 、App Widget 、Web App 的概念

       Widget最初的概念是98年一個叫Rose的蘋果工程師提出,直到2003年的時候才正式為大家所知,不過隨後無數大公司都開始接受並應用這一思路。現在我們看到在蘋果系統裡按下F4彈出的Dashboard裡的小工具叫Widget,在Windows 7裡側邊欄上的那些漂亮的小工具叫Gadget(widget變體?),除此以外還有yahoo Widget等等Widget產品。他們有一個共同的特點就是採用前台Web開發用的技術(譬如HTML、CSS、Javascript)來製作的小工具、小組件。

       在Android系統裡,幾乎每個可視化的View組件都叫Widget,起這個名字可能當時是為了趕時髦。

       App Widget是從Android 1.5以後才有的東東,就是可以放在Android案頭上的應用程式小工具。這一點上看他的功能很像windows的側邊欄小工具,可惜的是他的採用技術並不是HTML等技術。當然App Widget才是我們本講的主角,本來他應該順理成章叫做Widget的,至少也要叫做Gadget吧,可惜這個名字已經被他自己的系統佔用了,所以只好改名叫App Widget。

       最後講一下Web App 或者說是Android Web Application,也許叫mobile web application 更準確些。我們發現現在智能機系統平台很多,譬如iOS、Android、Windows Phone 、WebOS、BlackBerry等等,它們採用的技術架構也各不相同,有沒有辦法寫一個程式在各個系統上都能運行呢?答案是肯定的,寫基於Webkit的瀏覽器的應用即可。我們使用 HTML5、CSS3、JavaScript、WebKit 等技術來寫的Web Application也許是今後的一個大潮流也說不準啊。有機會我們再講講Android Web Application 的開發。

       二、App Widget 的簡單例子:Hello App Widget

       App Widget的技術實現有那麼一點點繞,我們用一個最簡單的例子Hello App Widget來操作一遍,然後再針對這個例子做講解,也許你會理解的更快些。

       1、建立一個項目 Lesson35_HelloAppWidget ,注意建立時可以不選Create Activity。

       2、準備好一個Widget的顯示布局檔案 layout/widget.xml,內容如下:

XML/HTML代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center">  
  3. <textview android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/textView1" android:text="歡迎進入App Widget的世界!" android:textcolor="#ff0000ff">  
  4. </textview></linearlayout>    

       3、準備好一個Widget的設定檔 xml/provider_info.xml,該檔案配置了widget可以佔用的螢幕長寬、更新頻率,所顯示的布局檔案(就是上面的那個布局檔案)等,其內容如下:

XML/HTML代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- appwidget-provider Widget的設定檔  -->  
  3. <!-- android:minWidth 最小寬度 -->  
  4. <!-- android:minHeight 最小高度 -->  
  5. <!-- android:updatePeriodMillis 組件更新頻率(毫秒) -->  
  6. <!-- android:initialLayout 組件布局XML的位置 -->  
  7. <!-- android:configure Widget設定用Activity -->  
  8. <appwidget -provider="" xmlns:android="http://schemas.android.com/apk/res/android" android:initiallayout="@layout/widget" android:updateperiodmillis="86400000" android:minheight="72dp" android:minwidth="294dp">  
  9. </appwidget>  

       4、準備好一個處理widget請求的Java檔案,basic.android.lesson35包下的HelloWidgetProvider,他繼承了AppWidgetProvider類,在本例中沒有任何請求處理的具體代碼,我在java檔案中寫了大量注釋,方便你的理解。內容如下:

Java代碼
  1. package basic.android.lesson35;   
  2.   
  3. import android.appwidget.AppWidgetManager;   
  4. import android.appwidget.AppWidgetProvider;   
  5. import android.content.Context;   
  6. import android.content.Intent;   
  7. import android.util.Log;   
  8.   
  9. // AppWidgetProvider 是 BroadcastReceiver 的子類,本質是個 廣播接收器,它專門用來接收來自 Widget組件的各種請求(用Intent傳遞過來),所以如果讓我給他起名的話 我會給他命名為AppWidgetReceiver,每一個Widget都要有一個AppWidgetProvider.   
  10. public class HelloWidgetProvider extends AppWidgetProvider {   
  11.   
  12.     //每個請求都會傳遞給onReceive方法,該方法根據Intent參數中的action類型來決定自己處理還是分發給下面四個特殊的方法。   
  13.     @Override  
  14.     public void onReceive(Context context, Intent intent) {   
  15.   
  16.         Log.i("yao", "HelloWidgetProvider --> onReceive");   
  17.         super.onReceive(context, intent);   
  18.     }   
  19.   
  20.     //如果Widget自動更新時間到了、或者其他會導致Widget發生變化的事件發生,或者說Intent的值是android.appwidget.action.APPWIDGET_UPDATE,那麼會調用onUpdate,下面三個方法類似   
  21.     @Override  
  22.     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {   
  23.         //AppWidgetManager 顧名思義是AppWidget的管理器,appWidgetIds 案頭上所有的widget都會被分配一個唯一的ID標識,那麼這個數組就是他們的列表   
  24.         Log.i("yao", "HelloWidgetProvider --> onUpdate");   
  25.         super.onUpdate(context, appWidgetManager, appWidgetIds);   
  26.     }   
  27.   
  28.     //當一個App Widget從案頭上刪除時調用   
  29.     @Override  
  30.     public void onDeleted(Context context, int[] appWidgetIds) {   
  31.         Log.i("yao", "HelloWidgetProvider --> onDeleted");   
  32.         super.onDeleted(context, appWidgetIds);   
  33.     }   
  34.   
  35.     //當這個App Widget第一次被放在案頭上時調用(同一個App Widget可以被放在案頭上多次,所以會有這個說法)   
  36.     @Override  
  37.     public void onEnabled(Context context) {   
  38.         Log.i("yao", "HelloWidgetProvider --> onEnabled");   
  39.         super.onEnabled(context);   
  40.     }   
  41.   
  42.     //當這個App Widget的最後一個執行個體被從案頭上移除時會調用該方法。   
  43.     @Override  
  44.     public void onDisabled(Context context) {   
  45.         Log.i("yao", "HelloWidgetProvider --> onDisabled");   
  46.         super.onDisabled(context);   
  47.     }   
  48.   
  49. }  

       5、配置AndroidManifest.xml檔案,增加一個receiver標籤,這個標籤看起來很像前面講的BroadReceiver的配置,具體內容如下:

XML/HTML代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionname="1.0" android:versioncode="1" package="basic.android.lesson35">  
  3.     <uses -sdk="" android:minsdkversion="7">  
  4.   
  5.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  6.   
  7.         <!-- receiver的 android:name指向的是widget的要求處理常式或者說請求接收者 -->  
  8.         <receiver android:label="Hello,App Widget" android:name=".HelloWidgetProvider">  
  9.             <intent -filter="">  
  10.                 <!-- widget預設的事件action -->  
  11.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>  
  12.             </intent>  
  13.             <!-- widget中繼資料,name是寫死的,resource指的是widget的設定檔 -->  
  14.             <meta -data="" android:name="android.appwidget.provider" android:resource="@xml/provider_info">  
  15.         </receiver>  
  16.     </application>  
  17. </uses></manifest>  

       6、編譯並運行程式,我們知道這種Widget程式,即使裝完了也不會在程式列表中出現,因為它根本就沒有main Activity,下面我給不清楚的同學說一下如何把一個widget放到案頭上。

       在模擬器上案頭上長按,等待彈出下面對話方塊:

       選擇視窗小組件:

       選擇Hello,App Widget:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.