随笔-1  评论-68  文章-98  trackbacks-0

作者:徐建祥(netpirate@gmail.com)
时间: 2010/12/15
来自: http://www.anymobile.org

Widget杂谈:最早Widget是指在PC的桌面上的小窗口程序;Web上的先行者似乎是Yahoo!;当然,OPhone也搞了一套Widget,HTML+CSS的东东。

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

需求:

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

(参考效果图)

设计思路:

(参考设计序列图)

代码:

Java:

    /src/org.anymobile.demo.Globals \\Intent.action 声明

     /src/org.anymobile.demo.service.UpdateService extends Service \\同步、更新Widget布局数据的Service

     /src/org.anymobile.demo.widget.UpdateAppWidgetProvider extends AppWidgetProvider \\Widget,接收器

XML:

    /res/layout/update_appwidget.xml \\布局设计

    /res/values/strings.xml \\常量声明

    /res/xml/update_appwidget_info.xml \\app widget定义

    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.       
  9. </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>  
  14.       
  15.     <LinearLayout   
  16.         android:id="@+id/app_widget_body"   
  17.         android:orientation="horizontal"   
  18.         android:background="@drawable/widget_body"   
  19.         android:layout_width="fill_parent"   
  20.         android:layout_height="100dip">  
  21.        <LinearLayout   
  22.             android:id="@+id/app_widget_message"   
  23.             android:layout_width="fill_parent"   
  24.             android:layout_height="fill_parent">  
  25.             <TextView  
  26.                 android:id="@+id/widget_message"  
  27.                 android:text="@string/app_widget_error_message"  
  28.                 android:paddingRight="5dip"  
  29.                 android:paddingLeft="5dip"  
  30.                 android:layout_width="wrap_content"  
  31.                 android:layout_height="wrap_content">  
  32.             </TextView>  
  33.         </LinearLayout>  
  34.     </LinearLayout>  
  35.       
  36.     <LinearLayout  
  37.         android:id="@+id/app_widget_bottom"  
  38.         android:gravity="right"  
  39.         android:layout_width="fill_parent"  
  40.         android:layout_height="wrap_content">  
  41.     </LinearLayout>  
  42. </LinearLayout>  

#Globals.java

  1. package org.anymobile.demo;  

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

#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. import org.anymobile.demo.Globals;  
  13. import org.anymobile.demo.R;  
  14. import org.anymobile.demo.service.UpdateService;  

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

日志:

#init

12-15 19:23:09.479 D/ANYMOBILE-DEMO--UpdateAppWidgetProvider(  585): onReceive() android.appwidget.action.APPWIDGET_UPDATE

12-15 19:23:09.509 D/ANYMOBILE-DEMO--UpdateAppWidgetProvider(  585): onUpdate()

12-15 19:23:09.549 D/ANYMOBILE-DEMO--UpdateService(  585): onCreate()

12-15 19:23:09.579 D/ANYMOBILE-DEMO--UpdateService(  585): onStart()

#add widget

12-15 19:24:23.780 D/ANYMOBILE-DEMO--UpdateAppWidgetProvider(  585): onReceive() android.appwidget.action.APPWIDGET_UPDATE

12-15 19:24:23.780 D/ANYMOBILE-DEMO--UpdateAppWidgetProvider(  585): onUpdate()

12-15 19:24:23.850 D/ANYMOBILE-DEMO--UpdateService(  585): onStart()

#receive software event, reload and update widget

12-15 19:24:58.150 D/ANYMOBILE-DEMO--UpdateService(  585): onReceive() Activation

12-15 19:24:58.150 D/ANYMOBILE-DEMO--UpdateService(  585): doReload()

12-15 19:24:58.150 D/ANYMOBILE-DEMO--UpdateService(  585): notifyWidget()

12-15 19:24:58.200 D/ANYMOBILE-DEMO--UpdateService(  585): buildUpdate: layoutId = 2130903068; count = 11; id = 0

#click widget button, new start the bind service

12-15 19:25:49.260 D/ANYMOBILE-DEMO--UpdateService(  585): onStart()

12-15 19:24:58.150 D/ANYMOBILE-DEMO--UpdateService(  585): notifyWidget()

12-15 19:24:58.200 D/ANYMOBILE-DEMO--UpdateService(  585): buildUpdate: layoutId = 2130903068; count = 11; id = 0

OVER!

参考:


com.android.music/.MediaAppWidgetProvider

com.android.music/.MediaPlaybackService

posted on 2010-12-15 21:44 Xu Jianxiang 阅读(890) 评论(0)  编辑  收藏 所属分类: Android

只有注册用户登录后才能发表评论。


网站导航: