解析Android Widget设计与开发

移动开发
Android Widget设计与开发是本文要介绍的内容,文章中很详细的讲解了Android Widget设计的思路,具体内容的实现来看详细代码。

Android Widget设计与开发是本文要介绍的内容,最早Widget是指在PC的桌面上的小窗口程序;Web上的先行者似乎是Yahoo!;当然,OPhone也搞了一套Widget,HTML+CSS的东东。

我们这里谈的所谓Widget,就是窗口小部件,Android SDK从1.5版本开始支持AppWidget framework,返个框架允许开发者开发Widgets,这些Widgets可以被用户通过长按桌面进行添加,与应用程序进行数据交互。

需求:

在桌面上开发一个Widget,可以实时显示IM软件的状态更新变化;可以通过左右按钮,查看上次或下调更新内容。

(参考效果图)

Android Widget设计与开发

设计思路:

(参考设计序列图)

Android Widget设计与开发

代码:

Java:

  1. /src/org.anymobile.demo.Globals \\Intent.action 声明  
  2.  
  3.  /src/org.anymobile.demo.service.UpdateService extends Service \\同步、更新Widget布局数据的Service  
  4.  
  5.  /src/org.anymobile.demo.widget.UpdateAppWidgetProvider extends AppWidgetProvider \\Widget,接收器 

XML:

  1. /res/layout/update_appwidget.xml \\布局设计  
  2. /res/values/strings.xml \\常量声明  
  3. /res/xml/update_appwidget_info.xml \\app widget定义  
  4. AndroidManifest.xml 

#AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>     
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"     
  3.       package="org.anymobile.demo"     
  4.       android:versionCode="1"     
  5.       android:versionName="1.0">     
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">     
  7.              
  8.         <receiver android:name=".widget.UpdateAppWidgetProvider"     
  9.                   android:label="@string/app_widget_label" >     
  10.             <intent-filter>     
  11.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />     
  12.             </intent-filter>     
  13.             <meta-data android:name="android.appwidget.provider"      
  14.                        android:resource="@xml/update_appwidget_info" />     
  15.         </receiver>     
  16.              
  17.         <service android:name=".service.UpdateService"  android:label="@string/app_name">     
  18.             <intent-filter>     
  19.                 <action android:name="org.anymobile.demo.service.IMM_UPDATE_SERVICE" />     
  20.                 <category android:name="android.intent.category.DEFAULT" />     
  21.             </intent-filter>     
  22.         </service>     
  23.     </application>     
  24. </manifest>     

#strings.xml

  1. <?xml version="1.0" encoding="utf-8"?>     
  2. <resources>     
  3.     <string name="app_name">AnymobileDemo</string>     
  4.          
  5.     <string name="app_widget_label">AnymobileDemo Widget</string>     
  6.     <string name="app_widget_title">Updates</string>     
  7.     <string name="app_widget_error_message">No messages, please check to login.</string>     
  8. </resources>    

#update_appwidget_info.xml

  1. <?xml version="1.0" encoding="utf-8"?>     
  2. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"     
  3.     android:minWidth="294dip"     
  4.     android:minHeight="120dip"     
  5.     android:updatePeriodMillis="0"     
  6.     android:initialLayout="@layout/update_appwidget">     
  7. </appwidget-provider>    

#update_appwidget.xml

  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.     <LinearLayout      
  7.         android:id="@+id/app_widget_top"      
  8.         android:gravity="center_vertical"     
  9.         android:orientation="horizontal"      
  10.         android:background="@drawable/widget_titlebar"      
  11.         android:layout_width="fill_parent"      
  12.         android:layout_height="wrap_content">     
  13.     </LinearLayout>    
  1.     <LinearLayout      
  2.         android:id="@+id/app_widget_body"      
  3.         android:orientation="horizontal"      
  4.         android:background="@drawable/widget_body"      
  5.         android:layout_width="fill_parent"      
  6.         android:layout_height="100dip">     
  7.        <LinearLayout      
  8.             android:id="@+id/app_widget_message"      
  9.             android:layout_width="fill_parent"      
  10.             android:layout_height="fill_parent">     
  11.             <TextView     
  12.                 android:id="@+id/widget_message"     
  13.                 android:text="@string/app_widget_error_message"     
  14.                 android:paddingRight="5dip"     
  15.                 android:paddingLeft="5dip"     
  16.                 android:layout_width="wrap_content"     
  17.                 android:layout_height="wrap_content">     
  18.             </TextView>     
  19.         </LinearLayout>     
  20.     </LinearLayout>     
  21.          
  22.     <LinearLayout     
  23.         android:id="@+id/app_widget_bottom"     
  24.         android:gravity="right"     
  25.         android:layout_width="fill_parent"     
  26.         android:layout_height="wrap_content">     
  27.     </LinearLayout>     
  28. </LinearLayout>    

#Globals.java

package org.anymobile.demo;  

  1. public final class Globals     
  2. {     
  3.     public static final String ACTION_APP_WIDGET_SERVICE"org.anymobile.demo.service.IMM_UPDATE_SERVICE";     
  4.          
  5.     public static final String ACTION_APP_WIDGET_PREV   = "org.anymobile.demo.intent.action.APP_WIDGET_PREV";     
  6.     public static final String ACTION_APP_WIDGET_NEXT   = "org.anymobile.demo.intent.action.APP_WIDGET_NEXT";     
  7.          
  8.     public static final String ACTION_APP_WIDGET_RELOAD = "org.anymobile.demo.intent.action.APP_WIDGET_RELOAD";  }    

#p#

#UpdateService.java

  1. package org.anymobile.demo.service;    
  2.  
  3. import java.util.ArrayList;    
  4. import android.app.Service;     
  5. import android.appwidget.AppWidgetManager;     
  6. import android.content.BroadcastReceiver;     
  7. import android.content.ComponentName;     
  8. import android.content.Context;     
  9. import android.content.Intent;     
  10. import android.content.IntentFilter;     
  11. import android.os.IBinder;     
  12. import android.util.Log;     
  13. import android.view.View;     
  14. import android.widget.RemoteViews;    
  15. import org.anymobile.demo.Globals;     
  16. import org.anymobile.demo.R;     
  17. import org.anymobile.demo.widget.UpdateAppWidgetProvider;    
  18.     
  19. public class UpdateService extends Service     
  20. {     
  21.     public static final String TAG = "ANYMOBILE-DEMO--UpdateService";     
  22.          
  23.     private ArrayList<String> mList;     
  24.     private int mCount;     
  25.     private int mId;     
  26.          
  27.     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver()     
  28.     {     
  29.         @Override     
  30.         public void onReceive(Context context, Intent intent)     
  31.         {     
  32.             String action = intent.getAction();     
  33.             Log.d(TAG, "onReceive() " + action);     
  34.                  
  35.             if (action.equals(Globals.ACTION_APP_WIDGET_RELOAD))     
  36.             {     
  37.                 doReload();     
  38.             }     
  39.         }     
  40.     };     
  41.     @Override     
  42.     public void onCreate()     
  43.     {     
  44.         Log.d(TAG, "onCreate()");     
  45.         super.onCreate();     
  46.              
  47.         reloadQueue();     
  48.              
  49.         IntentFilter filter = new IntentFilter();     
  50.         filter.addAction(Globals.ACTION_APP_WIDGET_RELOAD);     
  51.         registerReceiver(mIntentReceiver, filter);     
  52.     }     
  53.          
  54.     @Override     
  55.     public void onStart(Intent intent, int startId)     
  56.     {     
  57.         super.onStart(intent, startId);     
  58.         String action = intent.getAction();     
  59.         Log.d(TAG, "onStart() " + action);     
  60.         if (action.equals(Globals.ACTION_APP_WIDGET_PREV))     
  61.         {     
  62.             doPrev();     
  63.         }     
  64.         else if (action.equals(Globals.ACTION_APP_WIDGET_NEXT))     
  65.         {     
  66.             doNext();     
  67.         }     
  68.         else// if (action.equals(Globals.ACTION_APP_WIDGET_SERVICE))     
  69.         {     
  70.             notifyWidget();     
  71.         }     
  72.     }     
  73.          
  74.     private void notifyWidget()     
  75.     {     
  76.         Log.d(TAG, "notifyWidget()");     
  77.              
  78.         ComponentName widget = new ComponentName(this, UpdateAppWidgetProvider.class);     
  79.         RemoteViews updateViews = buildUpdate(this);     
  80.              
  81.         AppWidgetManager manager = AppWidgetManager.getInstance(this);     
  82.         manager.updateAppWidget(widget, updateViews);     
  83.     }     
  84.     @Override     
  85.     public void onDestroy()     
  86.     {     
  87.         Log.d(TAG, "onDestroy()");     
  88.              
  89.         unregisterReceiver(mIntentReceiver);     
  90.              
  91.         super.onDestroy();     
  92.     }     
  93.     @Override     
  94.     public IBinder onBind(Intent intent)     
  95.     {     
  96.         Log.d(TAG, "onBind()");     
  97.         return null;     
  98.     }     
  99.          
  100.     private RemoteViews buildUpdate(Context context)     
  101.     {     
  102.         RemoteViews updateViews =      
  103.             new RemoteViews(context.getPackageName(), R.layout.update_appwidget);     
  104.         String item = null;     
  105.              
  106.         if (mCount > 0)     
  107.         {     
  108.             item = mList.get(mId);     
  109.             if (item != null)     
  110.             {     
  111.                 updateViews.setViewVisibility(R.id.app_widget_content, View.GONE);     
  112.                 updateViews.setViewVisibility(R.id.app_widget_message, View.VISIBLE);     
  113.                      
  114. //              updateViews.setViewVisibility(R.id.app_widget_content, View.VISIBLE);     
  115. //              updateViews.setViewVisibility(R.id.app_widget_message, View.GONE);     
  116. //                   
  117. //              updateViews.setImageViewResource(R.id.update_appwidget_icon, item.getTypeIconId());     
  118. //              updateViews.setTextViewText(R.id.update_appwidget_name, item.getNickName());     
  119. //              updateViews.setTextViewText(R.id.update_appwidget_time, item.getModifyTime());     
  120. //              updateViews.setTextViewText(R.id.update_appwidget_content, item.getMessage());     
  121.                      
  122.                 updateViews.setTextViewText(R.id.widget_message, item);     
  123.             }     
  124.         }     
  125.         if (item == null)     
  126.         {     
  127.             updateViews.setViewVisibility(R.id.app_widget_content, View.GONE);     
  128.             updateViews.setViewVisibility(R.id.app_widget_message, View.VISIBLE);     
  129.                  
  130.             updateViews.setTextViewText(R.id.widget_message,      
  131.                 context.getText(R.string.app_widget_error_message));     
  132.         }     
  133.         Log.d(TAG, "buildUpdate: layoutId = " + updateViews.getLayoutId() +      
  134.             "; count = " + mCount + "id = " + mId);     
  135.              
  136.         return updateViews;     
  137.     }     
  138.          
  139.     private void doReload()     
  140.     {     
  141.         Log.d(TAG, "doReload()");     
  142.         reloadQueue();     
  143.              
  144.         notifyWidget();     
  145.     }     
  146.          
  147.     private void reloadQueue()     
  148.     {     
  149.         mList = new ArrayList<String>();     
  150.         String[] arr = {"aa", "bb", "cc", "dd"};     
  151.         for (int i = 0; i < arr.length; i++)     
  152.         {     
  153.             mList.add(arr[i]);     
  154.         }     
  155.              
  156.         if (mList != null)     
  157.         {     
  158.             mCount = mList.size();     
  159.         }     
  160.         else     
  161.         {     
  162.             mCount = 0;     
  163.         }     
  164.         mId = 0;     
  165.              
  166.         //TODO check login and poll updates from buddie list     
  167.     }     
  168.          
  169.     private void doPrev()     
  170.     {     
  171.         Log.d(TAG, "doPrev()");     
  172.         mId -1;     
  173.         if (mId < 0)     
  174.         {     
  175.             mId = mCount - 1;     
  176.         }     
  177.         notifyWidget();     
  178.     }     
  179.          
  180.     private void doNext()     
  181.     {     
  182.         Log.d(TAG, "doNext()");     
  183.         mId += 1;     
  184.         if (mId > mCount - 1)     
  185.         {     
  186.             mId = 0;     
  187.         }     
  188.         notifyWidget();     
  189.     }     
  190. }    

#UpdateAppWidgetProvider.java

  1. package org.anymobile.demo.widget;    
  2.     
  3. import android.app.PendingIntent;     
  4. import android.appwidget.AppWidgetManager;     
  5. import android.appwidget.AppWidgetProvider;     
  6. import android.content.ComponentName;     
  7. import android.content.Context;     
  8. import android.content.Intent;     
  9. import android.util.Log;     
  10. import android.view.View;     
  11. import android.widget.RemoteViews;     
  12.  
  13.  
  14. import org.anymobile.demo.Globals;     
  15. import org.anymobile.demo.R;     
  16. import org.anymobile.demo.service.UpdateService;     
  17.  
  18.  
  19. public class UpdateAppWidgetProvider extends AppWidgetProvider     
  20. {     
  21.     public static final String TAG = "ANYMOBILE-DEMO-UpdateAppWidgetProvider";     
  22.          
  23.     public static final String APP_WIDGET_UPDATE = "appwidgetupdate";     
  24.     public static final ComponentName APPWIDGET_COMPONENT_NAME =     
  25.         new ComponentName("org.anymobile.demo",      
  26.             "org.anymobile.demo.widget.UpdateAppWidgetProvider");     
  27.     @Override     
  28.     public void onReceive(Context context, Intent intent)     
  29.     {     
  30.         Log.d(TAG, "onReceive() " + intent.getAction());     
  31.         super.onReceive(context, intent);     
  32.     }     
  33.     @Override     
  34.     public void onEnabled(Context context)     
  35.     {     
  36.         Log.d(TAG, "onEnabled()");     
  37.         super.onEnabled(context);     
  38.     }     
  39.     @Override     
  40.     public void onDisabled(Context context)     
  41.     {     
  42.         Log.d(TAG, "onDisabled()");     
  43.         super.onDisabled(context);     
  44.     }     
  45.     @Override     
  46.     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)     
  47.     {     
  48.         Log.d(TAG, "onUpdate()");     
  49.              
  50.         defaultAppWidget(context, appWidgetIds);     
  51.              
  52.         context.startService(new Intent(Globals.ACTION_APP_WIDGET_SERVICE));     
  53.     }     
  54.          
  55.     private void defaultAppWidget(Context context, int[] appWidgetIds)     
  56.     {     
  57.         final RemoteViews views =      
  58.             new RemoteViews(context.getPackageName(), R.layout.update_appwidget);     
  59.              
  60.         views.setViewVisibility(R.id.app_widget_content, View.GONE);     
  61.         views.setViewVisibility(R.id.app_widget_message, View.VISIBLE);     
  62.              
  63.         // Link actions buttons to intents     
  64.         linkButtons(context, views);     
  65.              
  66.         pushUpdate(context, appWidgetIds, views);     
  67.     }     
  68.          
  69.     private void linkButtons(Context context, RemoteViews views)     
  70.     {     
  71.         Intent intent;     
  72.         PendingIntent pendingIntent;     
  73.         final ComponentName serviceName = new ComponentName(context, UpdateService.class);     
  74.              
  75.         intent = new Intent(Globals.ACTION_APP_WIDGET_PREV);     
  76.         intent.setComponent(serviceName);     
  77.         pendingIntent = PendingIntent.getService(context,     
  78.                 0 /* no requestCode */, intent, 0 /* no flags */);     
  79.         views.setOnClickPendingIntent(R.id.widget_btn_prev_page, pendingIntent);     
  80.         intent = new Intent(Globals.ACTION_APP_WIDGET_NEXT);     
  81.         intent.setComponent(serviceName);     
  82.         pendingIntent = PendingIntent.getService(context,     
  83.                 0 /* no requestCode */, intent, 0 /* no flags */);     
  84.         views.setOnClickPendingIntent(R.id.widget_btn_next_page, pendingIntent);     
  85.     }     
  86.          
  87.     private void pushUpdate(Context context, int[] appWidgetIds, RemoteViews views)     
  88.     {     
  89.         final AppWidgetManager gm = AppWidgetManager.getInstance(context);     
  90.         if (appWidgetIds != null)     
  91.         {     
  92.             gm.updateAppWidget(appWidgetIds, views);     
  93.         }     
  94.         else     
  95.         {     
  96.             gm.updateAppWidget(APPWIDGET_COMPONENT_NAME, views);     
  97.         }     
  98.     }     
  99.          
  100.     void notifyChange(UpdateService service, String what)     
  101.     {     
  102.         //     
  103.     }     
  104. }    

日志:

  1. #init  
  2. 12-15 19:23:09.479 D/ANYMOBILE-DEMO--UpdateAppWidgetProvider(  585): onReceive() android.appwidget.action.APPWIDGET_UPDATE  
  3. 12-15 19:23:09.509 D/ANYMOBILE-DEMO--UpdateAppWidgetProvider(  585): onUpdate()  
  4. 12-15 19:23:09.549 D/ANYMOBILE-DEMO--UpdateService(  585): onCreate()  
  5. 12-15 19:23:09.579 D/ANYMOBILE-DEMO--UpdateService(  585): onStart()  
  6. #add widget  
  7. 12-15 19:24:23.780 D/ANYMOBILE-DEMO--UpdateAppWidgetProvider(  585): onReceive() android.appwidget.action.APPWIDGET_UPDATE  
  8. 12-15 19:24:23.780 D/ANYMOBILE-DEMO--UpdateAppWidgetProvider(  585): onUpdate()  
  9. 12-15 19:24:23.850 D/ANYMOBILE-DEMO--UpdateService(  585): onStart()  
  10. #receive software event, reload and update widget  
  11. 12-15 19:24:58.150 D/ANYMOBILE-DEMO--UpdateService(  585): onReceive() Activation  
  12. 12-15 19:24:58.150 D/ANYMOBILE-DEMO--UpdateService(  585): doReload()  
  13. 12-15 19:24:58.150 D/ANYMOBILE-DEMO--UpdateService(  585): notifyWidget()  
  14. 12-15 19:24:58.200 D/ANYMOBILE-DEMO--UpdateService(  585): buildUpdate: layoutId = 2130903068count = 11id = 0 
  15. #click widget button, new start the bind service  
  16. 12-15 19:25:49.260 D/ANYMOBILE-DEMO--UpdateService(  585): onStart()  
  17. 12-15 19:24:58.150 D/ANYMOBILE-DEMO--UpdateService(  585): notifyWidget()  
  18. 12-15 19:24:58.200 D/ANYMOBILE-DEMO--UpdateService(  585): buildUpdate: layoutId = 2130903068count = 11id = 0 
  19. OVER! 

小结:解析Android Widget设计与开发的内容介绍完了,希望通过Android Widget设计内容的学习能对你有所帮助!

责任编辑:zhaolei 来源: CSDN博客
相关推荐

2011-09-09 10:00:20

Android Wid开发

2011-09-07 17:54:40

Android Wid开发

2011-09-07 14:25:53

Android Wid设计

2011-03-14 09:55:25

AndroidWidget

2010-07-13 09:02:19

Widget开发

2011-09-07 14:01:41

Android Wid实例

2010-07-13 09:08:27

Widget开发

2011-09-09 20:14:58

Android Wid

2010-07-23 08:54:02

2011-09-08 15:40:45

Android Wid组件

2010-04-23 11:21:05

Widget开发

2011-09-07 13:00:36

2011-09-08 13:11:07

Android Wid实例

2011-09-07 11:15:25

2011-09-07 13:30:48

Android WidTabWidget

2011-09-07 14:34:55

Android Wid控件

2011-09-09 17:59:26

QT Widget

2011-09-08 14:21:37

jQueryWidget

2011-09-08 16:17:45

Widget

2011-09-09 19:23:52

Widget
点赞
收藏

51CTO技术栈公众号