系统小组件

Android Widget 小部件(四---完结) 使用ListView、GridView、StackView、ViewFlipper展示Widget

分类: Android

2014-08-10 23:13

2596人阅读

评论(1)

收藏

举报

android widget

官方有话这样说:

A RemoteViews object (and, consequently, an App Widget) can support the following layout classes:

  • FrameLayout
  • LinearLayout
  • RelativeLayout
  • And the following widget classes:

  • AnalogClock
  • Button
  • Chronometer
  • ImageButton
  • ImageView
  • ProgressBar
  • TextView
  • ViewFlipper
  • ListView
  • GridView
  • StackView
  • AdapterViewFlipper
  • Descendants of these classes are not supported.不支持这些类的后代

    接下来的示例说明怎么样实现 使用ListView、GridView、StackView、ViewFlipper创建AppWidget

    menifest

    [html] view plain copy print ?

    1.   
    2.                
    3.                    
    4.                    
    5.                    
    6.                
    7.              
    8.                  android:resource="@xml/set_widget_provider"/>  
    9.   

    [html] view plain copy print ?

    1.   
    2.                
    3.                    
    4.                    
    5.                    
    6.                
    7.              
    8.                  android:resource="@xml/set_widget_provider"/>  
    9.   


    res/xml/set_widget_provider.xml

    [html] view plain copy print ?

    1.   
    2.     android:minWidth="250dp"  
    3.     android:minHeight="180dp"  
    4.     android:updatePeriodMillis="5000"  
    5.     android:previewImage="@drawable/ic_launcher"  
    6.     android:initialLayout="@layout/collections_view_widget"  
    7.     android:resizeMode="horizontal|vertical"  
    8.     android:autoAdvanceViewId="@id/viewflipper"  >  
    9.       
    10.   

    [html] view plain copy print ?

    1.   
    2.     android:minWidth="250dp"  
    3.     android:minHeight="180dp"  
    4.     android:updatePeriodMillis="5000"  
    5.     android:previewImage="@drawable/ic_launcher"  
    6.     android:initialLayout="@layout/collections_view_widget"  
    7.     android:resizeMode="horizontal|vertical"  
    8.     android:autoAdvanceViewId="@id/viewflipper"  >  
    9.       
    10.   


    layout/collections_view_widget.xml

    [html] view plain copy print ?

    1.   
    2.     android:layout_width="match_parent"  
    3.     android:layout_height="match_parent"  
    4.     android:orientation="vertical" >  
    5.   
    6.     
    7.         android:layout_width="fill_parent"  
    8.         android:layout_height="wrap_content" >  
    9.   
    10.         
    11.             android:id="@+id/btn_listview"  
    12.             android:layout_width="0dp"  
    13.             android:layout_height="wrap_content"  
    14.             android:layout_weight="1"  
    15.             android:text="listview" />  
    16.   
    17.         
    18.             android:id="@+id/btn_gridview"  
    19.             android:layout_width="0dp"  
    20.             android:layout_height="wrap_content"  
    21.             android:layout_weight="1"  
    22.             android:text="gridview" />  
    23.   
    24.         
    25.             android:id="@+id/btn_stackview"  
    26.             android:layout_width="0dp"  
    27.             android:layout_height="wrap_content"  
    28.             android:layout_weight="1"  
    29.             android:text="stackview" />  
    30.   
    31.         
    32.             android:id="@+id/btn_viewflipper"  
    33.             android:layout_width="0dp"  
    34.             android:layout_height="wrap_content"  
    35.             android:layout_weight="1"  
    36.             android:text="viewflipper" />  
    37.       
    38.   
    39.     
    40.         xmlns:android="http://schemas.android.com/apk/res/android"  
    41.         android:layout_width="match_parent"  
    42.         android:layout_height="match_parent"  
    43.         android:background="#80000000" >  
    44.         />  
    45.   
    46.         
    47.             android:id="@+id/listview"  
    48.             android:layout_width="fill_parent"  
    49.             android:layout_height="fill_parent" />  
    50.   
    51.         
    52.             android:id="@+id/gridview"  
    53.             android:layout_width="fill_parent"  
    54.             android:layout_height="fill_parent"  
    55.             android:numColumns="2"  
    56.             android:visibility="gone" />  
    57.   
    58.         
    59.             android:id="@+id/stackview"  
    60.             android:layout_width="fill_parent"  
    61.             android:layout_height="fill_parent"  
    62.             android:visibility="gone" />  
    63.   
    64.         
    65.             android:id="@+id/viewflipper"  
    66.             android:layout_width="fill_parent"  
    67.             android:layout_height="fill_parent"  
    68.             android:autoStart="true"  
    69.             android:flipInterval="2000"  
    70.             android:visibility="gone" >  
    71.   
    72.             
    73.                 android:id="@+id/iv1"  
    74.                 android:layout_width="fill_parent"  
    75.                 android:layout_height="fill_parent"  
    76.                 android:background="@drawable/a11"/>  
    77.             
    78.                 android:id="@+id/iv2"  
    79.                 android:layout_width="fill_parent"  
    80.                 android:layout_height="fill_parent"  
    81.                 android:background="@drawable/a2"/>  
    82.             
    83.                 android:id="@+id/iv3"  
    84.                 android:layout_width="fill_parent"  
    85.                 android:layout_height="fill_parent"  
    86.                 android:background="@drawable/a3" />  
    87.             
    88.                 android:id="@+id/iv4"  
    89.                 android:layout_width="fill_parent"  
    90.                 android:layout_height="fill_parent"  
    91.                 android:background="@drawable/a4" />  
    92.           
    93.   
    94.         
    95.             android:id="@+id/tv_empty"  
    96.             android:layout_width="fill_parent"  
    97.             android:layout_height="fill_parent"  
    98.             android:gravity="center"  
    99.             android:text="Empty List"  
    100.             android:visibility="gone" />  
    101.       
    102.   
    103.   

    [html] view plain copy print ?
    1.   
    2.     android:layout_width="match_parent"  
    3.     android:layout_height="match_parent"  
    4.     android:orientation="vertical" >  
    5.   
    6.     
    7.         android:layout_width="fill_parent"  
    8.         android:layout_height="wrap_content" >  
    9.   
    10.         
    11.             android:id="@+id/btn_listview"  
    12.             android:layout_width="0dp"  
    13.             android:layout_height="wrap_content"  
    14.             android:layout_weight="1"  
    15.             android:text="listview" />  
    16.   
    17.         
    18.             android:id="@+id/btn_gridview"  
    19.             android:layout_width="0dp"  
    20.             android:layout_height="wrap_content"  
    21.             android:layout_weight="1"  
    22.             android:text="gridview" />  
    23.   
    24.         
    25.             android:id="@+id/btn_stackview"  
    26.             android:layout_width="0dp"  
    27.             android:layout_height="wrap_content"  
    28.             android:layout_weight="1"  
    29.             android:text="stackview" />  
    30.   
    31.         
    32.             android:id="@+id/btn_viewflipper"  
    33.             android:layout_width="0dp"  
    34.             android:layout_height="wrap_content"  
    35.             android:layout_weight="1"  
    36.             android:text="viewflipper" />  
    37.       
    38.   
    39.     
    40.         xmlns:android="http://schemas.android.com/apk/res/android"  
    41.         android:layout_width="match_parent"  
    42.         android:layout_height="match_parent"  
    43.         android:background="#80000000" >  
    44.         />  
    45.   
    46.         
    47.             android:id="@+id/listview"  
    48.             android:layout_width="fill_parent"  
    49.             android:layout_height="fill_parent" />  
    50.   
    51.         
    52.             android:id="@+id/gridview"  
    53.             android:layout_width="fill_parent"  
    54.             android:layout_height="fill_parent"  
    55.             android:numColumns="2"  
    56.             android:visibility="gone" />  
    57.   
    58.         
    59.             android:id="@+id/stackview"  
    60.             android:layout_width="fill_parent"  
    61.             android:layout_height="fill_parent"  
    62.             android:visibility="gone" />  
    63.   
    64.         
    65.             android:id="@+id/viewflipper"  
    66.             android:layout_width="fill_parent"  
    67.             android:layout_height="fill_parent"  
    68.             android:autoStart="true"  
    69.             android:flipInterval="2000"  
    70.             android:visibility="gone" >  
    71.   
    72.             
    73.                 android:id="@+id/iv1"  
    74.                 android:layout_width="fill_parent"  
    75.                 android:layout_height="fill_parent"  
    76.                 android:background="@drawable/a11"/>  
    77.             
    78.                 android:id="@+id/iv2"  
    79.                 android:layout_width="fill_parent"  
    80.                 android:layout_height="fill_parent"  
    81.                 android:background="@drawable/a2"/>  
    82.             
    83.                 android:id="@+id/iv3"  
    84.                 android:layout_width="fill_parent"  
    85.                 android:layout_height="fill_parent"  
    86.                 android:background="@drawable/a3" />  
    87.             
    88.                 android:id="@+id/iv4"  
    89.                 android:layout_width="fill_parent"  
    90.                 android:layout_height="fill_parent"  
    91.                 android:background="@drawable/a4" />  
    92.           
    93.   
    94.         
    95.             android:id="@+id/tv_empty"  
    96.             android:layout_width="fill_parent"  
    97.             android:layout_height="fill_parent"  
    98.             android:gravity="center"  
    99.             android:text="Empty List"  
    100.             android:visibility="gone" />  
    101.       
    102.   
    103.   


    集合的数据源 需要 继承 RemoteViewsService

    [java] view plain copy print ?
    1. package com.stone.service;  
    2.   
    3. import java.util.ArrayList;  
    4. import java.util.List;  
    5.   
    6. import android.app.PendingIntent;  
    7. import android.content.Context;  
    8. import android.content.Intent;  
    9. import android.os.Bundle;  
    10. import android.widget.RemoteViews;  
    11. import android.widget.RemoteViewsService;  
    12.   
    13. import com.stone.R;  
    14. import com.stone.receiver.WidgetSetProvider;  
    15.   
    16. /** 
    17.  * 继承自RemoteViewsService 必须重写onGetViewFactory 
    18.  * 该服务只是用来 创建 集合widget使用的数据源 
    19.  * @author stone 
    20.  */  
    21. public class WidgetSetService extends RemoteViewsService {  
    22.       
    23.     public WidgetSetService() {  
    24.           
    25.     }  
    26.       
    27.     @Override  
    28.     public RemoteViewsFactory onGetViewFactory(Intent intent) {  
    29.         return new WidgetFactory(this.getApplicationContext(), intent);  
    30.     }  
    31.       
    32.       
    33.     public class WidgetFactory implements RemoteViewsService.RemoteViewsFactory {  
    34.         private static final int mCount = 10;  
    35.         private Context mContext;  
    36.         private List mWidgetItems = new ArrayList();  
    37.           
    38.           
    39.         public WidgetFactory(Context context, Intent intent) {  
    40.             mContext = context;  
    41. //          mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,  
    42. //                  AppWidgetManager.INVALID_APPWIDGET_ID);  
    43.         }  
    44.           
    45.         @Override  
    46.         public void onCreate() {  
    47.             for (int i = 0; i < mCount; i++) {  
    48.                 mWidgetItems.add("item:" + i + "!");  
    49.             }  
    50.         }  
    51.   
    52.         @Override  
    53.         public void onDataSetChanged() {  
    54.             /* 
    55.              * appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listview); 
    56.              * 使用该通知更新数据源,会调用onDataSetChanged 
    57.              */  
    58.             System.out.println("----onDataSetChanged----");  
    59.         }  
    60.   
    61.         @Override  
    62.         public void onDestroy() {  
    63.             mWidgetItems.clear();  
    64.         }  
    65.   
    66.         @Override  
    67.         public int getCount() {  
    68.             return  mCount;  
    69.         }  
    70.   
    71.         @Override  
    72.         public RemoteViews getViewAt(int position) {  
    73.             RemoteViews views = new RemoteViews(mContext.getPackageName(), android.R.layout.simple_list_item_1);  
    74.             views.setTextViewText(android.R.id.text1, "item:" + position);  
    75.             System.out.println("RemoteViewsService----getViewAt" + position);  
    76.               
    77.               
    78.             Bundle extras = new Bundle();  
    79.             extras.putInt(WidgetSetProvider.EXTRA_ITEM, position);  
    80.             Intent fillInIntent = new Intent();  
    81.             fillInIntent.putExtras(extras);  
    82.             /* 
    83.              * android.R.layout.simple_list_item_1 --- id --- text1 
    84.              * listview的item click:将fillInIntent发送, 
    85.              * fillInIntent它默认的就有action 是provider中使用 setPendingIntentTemplate 设置的action 
    86.              */  
    87.             views.setOnClickFillInIntent(android.R.id.text1, fillInIntent);  
    88.               
    89.             return views;  
    90.         }  
    91.   
    92.         @Override  
    93.         public RemoteViews getLoadingView() {  
    94.             /* 在更新界面的时候如果耗时就会显示 正在加载... 的默认字样,但是你可以更改这个界面 
    95.              * 如果返回null 显示默认界面 
    96.              * 否则 加载自定义的,返回RemoteViews 
    97.              */  
    98.             return null;  
    99.         }  
    100.   
    101.         @Override  
    102.         public int getViewTypeCount() {  
    103.             return 1;  
    104.         }  
    105.   
    106.         @Override  
    107.         public long getItemId(int position) {  
    108.             return position;  
    109.         }  
    110.   
    111.         @Override  
    112.         public boolean hasStableIds() {  
    113.             return false;  
    114.         }  
    115.           
    116.     }  
    117.   
    118. }  
    [java] view plain copy print ?
    1. package com.stone.service;  
    2.   
    3. import java.util.ArrayList;  
    4. import java.util.List;  
    5.   
    6. import android.app.PendingIntent;  
    7. import android.content.Context;  
    8. import android.content.Intent;  
    9. import android.os.Bundle;  
    10. import android.widget.RemoteViews;  
    11. import android.widget.RemoteViewsService;  
    12.   
    13. import com.stone.R;  
    14. import com.stone.receiver.WidgetSetProvider;  
    15.   
    16. /** 
    17.  * 继承自RemoteViewsService 必须重写onGetViewFactory 
    18.  * 该服务只是用来 创建 集合widget使用的数据源 
    19.  * @author stone 
    20.  */  
    21. public class WidgetSetService extends RemoteViewsService {  
    22.       
    23.     public WidgetSetService() {  
    24.           
    25.     }  
    26.       
    27.     @Override  
    28.     public RemoteViewsFactory onGetViewFactory(Intent intent) {  
    29.         return new WidgetFactory(this.getApplicationContext(), intent);  
    30.     }  
    31.       
    32.       
    33.     public class WidgetFactory implements RemoteViewsService.RemoteViewsFactory {  
    34.         private static final int mCount = 10;  
    35.         private Context mContext;  
    36.         private List mWidgetItems = new ArrayList();  
    37.           
    38.           
    39.         public WidgetFactory(Context context, Intent intent) {  
    40.             mContext = context;  
    41. //          mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,  
    42. //                  AppWidgetManager.INVALID_APPWIDGET_ID);  
    43.         }  
    44.           
    45.         @Override  
    46.         public void onCreate() {  
    47.             for (int i = 0; i < mCount; i++) {  
    48.                 mWidgetItems.add("item:" + i + "!");  
    49.             }  
    50.         }  
    51.   
    52.         @Override  
    53.         public void onDataSetChanged() {  
    54.             /* 
    55.              * appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listview); 
    56.              * 使用该通知更新数据源,会调用onDataSetChanged 
    57.              */  
    58.             System.out.println("----onDataSetChanged----");  
    59.         }  
    60.   
    61.         @Override  
    62.         public void onDestroy() {  
    63.             mWidgetItems.clear();  
    64.         }  
    65.   
    66.         @Override  
    67.         public int getCount() {  
    68.             return  mCount;  
    69.         }  
    70.   
    71.         @Override  
    72.         public RemoteViews getViewAt(int position) {  
    73.             RemoteViews views = new RemoteViews(mContext.getPackageName(), android.R.layout.simple_list_item_1);  
    74.             views.setTextViewText(android.R.id.text1, "item:" + position);  
    75.             System.out.println("RemoteViewsService----getViewAt" + position);  
    76.               
    77.               
    78.             Bundle extras = new Bundle();  
    79.             extras.putInt(WidgetSetProvider.EXTRA_ITEM, position);  
    80.             Intent fillInIntent = new Intent();  
    81.             fillInIntent.putExtras(extras);  
    82.             /* 
    83.              * android.R.layout.simple_list_item_1 --- id --- text1 
    84.              * listview的item click:将fillInIntent发送, 
    85.              * fillInIntent它默认的就有action 是provider中使用 setPendingIntentTemplate 设置的action 
    86.              */  
    87.             views.setOnClickFillInIntent(android.R.id.text1, fillInIntent);  
    88.               
    89.             return views;  
    90.         }  
    91.   
    92.         @Override  
    93.         public RemoteViews getLoadingView() {  
    94.             /* 在更新界面的时候如果耗时就会显示 正在加载... 的默认字样,但是你可以更改这个界面 
    95.              * 如果返回null 显示默认界面 
    96.              * 否则 加载自定义的,返回RemoteViews 
    97.              */  
    98.             return null;  
    99.         }  
    100.   
    101.         @Override  
    102.         public int getViewTypeCount() {  
    103.             return 1;  
    104.         }  
    105.   
    106.         @Override  
    107.         public long getItemId(int position) {  
    108.             return position;  
    109.         }  
    110.   
    111.         @Override  
    112.         public boolean hasStableIds() {  
    113.             return false;  
    114.         }  
    115.           
    116.     }  
    117.   
    118. }  

    widgetprovider

    [java] view plain copy print ?
    1. package com.stone.receiver;  
    2.   
    3. import com.stone.R;  
    4. import com.stone.service.WidgetSetService;  
    5.   
    6. import android.app.PendingIntent;  
    7. import android.appwidget.AppWidgetManager;  
    8. import android.appwidget.AppWidgetProvider;  
    9. import android.content.ComponentName;  
    10. import android.content.Context;  
    11. import android.content.Intent;  
    12. import android.os.Bundle;  
    13. import android.text.TextUtils;  
    14. import android.text.format.DateUtils;  
    15. import android.view.View;  
    16. import android.widget.RemoteViews;  
    17. import android.widget.Toast;  
    18. import android.widget.ViewFlipper;  
    19.   
    20. /** 
    21.  * 使用了集合展示AppWidget 
    22.  * ListView、GridView、StackView 设置adapter,处理item点击 
    23.  * ViewFlipper 在RemoteViews中缺少支持,暂只能在它的布局文件中设置 轮换效果 
    24.  *              对于切换到哪一个子view的item事件不好处理,只能设置一个整体setPendingIntent 
    25.  * @author stone 
    26.  */  
    27. public class WidgetSetProvider extends AppWidgetProvider {  
    28.     public final static String CLICK_ACTION = "com.stone.action.clickset";  
    29.     public final static String CLICK_ITEM_ACTION = "com.stone.action.clickset.item";  
    30.     public final static String EXTRA_ITEM = "extra_item";  
    31.       
    32.     @Override  
    33.     public void onReceive(Context context, Intent intent) {  
    34.         super.onReceive(context, intent);  
    35.         System.out.println(intent.getAction());  
    36.         if (TextUtils.equals(CLICK_ACTION, intent.getAction())) {  
    37.             int extraType = intent.getIntExtra("view_tag", 0);  
    38.             if (extraType > 0) {  
    39.                 System.out.println("extra:::" + extraType);  
    40.                   
    41.                 switch (extraType) {  
    42.                 case 1:  
    43.                     updateWidget(context, R.id.listview, R.id.gridview, R.id.stackview, R.id.viewflipper);  
    44.                     break;  
    45.                 case 2:  
    46.                     updateWidget(context, R.id.gridview, R.id.listview, R.id.stackview, R.id.viewflipper);  
    47.                     break;  
    48.                 case 3:  
    49.                     updateWidget(context, R.id.stackview, R.id.gridview, R.id.listview, R.id.viewflipper);  
    50.                     break;  
    51.                 case 4:  
    52.                     updateWidget(context, R.id.viewflipper, R.id.gridview, R.id.stackview, R.id.listview);  
    53.                     break;  
    54.       
    55.                 default:  
    56.                     break;  
    57.                 }  
    58.             }   
    59.         } else if (TextUtils.equals(CLICK_ITEM_ACTION, intent.getAction())) {  
    60.             Bundle extras = intent.getExtras();  
    61.             int position = extras.getInt(WidgetSetProvider.EXTRA_ITEM, -1);  
    62.             if (position != -1) {  
    63.                 System.out.println("--点击了item---" + position);  
    64.                 System.out.println("");  
    65. //              Toast.makeText(context, "click item:" + position, 0).show();  
    66.             }  
    67.         }  
    68.     }  
    69.   
    70.     @Override  
    71.     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {  
    72.         RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.collections_view_widget);  
    73.           
    74.         Intent intent1 = new Intent(CLICK_ACTION);  
    75.         intent1.putExtra("view_tag", 1);  
    76.         PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 101, intent1, 0);  
    77.         views.setOnClickPendingIntent(R.id.btn_listview, pendingIntent1);  
    78.           
    79.         Intent intent2 = new Intent(CLICK_ACTION);  
    80.         intent2.putExtra("view_tag", 2);  
    81.         PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 102, intent2, 0);  
    82.         views.setOnClickPendingIntent(R.id.btn_gridview, pendingIntent2);  
    83.           
    84.         Intent intent3 = new Intent(CLICK_ACTION);  
    85.         intent3.putExtra("view_tag", 3);  
    86.         PendingIntent pendingIntent3 = PendingIntent.getBroadcast(context, 103, intent3, 0);  
    87.         views.setOnClickPendingIntent(R.id.btn_stackview, pendingIntent3);  
    88.           
    89.         Intent intent4 = new Intent(CLICK_ACTION);  
    90.         intent4.putExtra("view_tag", 4);  
    91.         PendingIntent pendingIntent4 = PendingIntent.getBroadcast(context, 104, intent4, 0);  
    92.         views.setOnClickPendingIntent(R.id.btn_viewflipper, pendingIntent4);  
    93.           
    94.         appWidgetManager.updateAppWidget(appWidgetIds, views);  
    95.           
    96.         System.out.println("setwidget update");  
    97.         super.onUpdate(context, appWidgetManager, appWidgetIds);  
    98.     }  
    99.   
    100.     @Override  
    101.     public void onAppWidgetOptionsChanged(Context context,   
    102.             AppWidgetManager appWidgetManager, int appWidgetId,  
    103.             Bundle newOptions) {  
    104.         super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,  
    105.                 newOptions);  
    106.     }  
    107.   
    108.     @Override  
    109.     public void onDeleted(Context context, int[] appWidgetIds) {  
    110.         super.onDeleted(context, appWidgetIds);  
    111.     }  
    112.   
    113.     @Override  
    114.     public void onEnabled(Context context) {  
    115.         super.onEnabled(context);  
    116.     }  
    117.   
    118.     @Override  
    119.     public void onDisabled(Context context) {  
    120.         super.onDisabled(context);  
    121.     }  
    122.       
    123.     private void updateWidget(Context context, int visible, int gone1, int gone2, int gone3) {  
    124.         //RemoteViews处理异进程中的View  
    125.         RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.collections_view_widget);  
    126.           
    127.         views.setViewVisibility(visible, View.VISIBLE);  
    128.         views.setViewVisibility(gone1, View.GONE);  
    129.         views.setViewVisibility(gone2, View.GONE);  
    130.         views.setViewVisibility(gone3, View.GONE);  
    131.           
    132.         if (visible != R.id.viewflipper) {//viewflipper 不是 继承自AbsListView  or  AdapterViewAnimator  的view  
    133.             Intent intent = new Intent(context, WidgetSetService.class);  
    134.             views.setRemoteAdapter(visible, intent);//设置集合的adapter为intent指定的service  
    135.             views.setEmptyView(visible, R.id.tv_empty);//指定集合view为空时显示的view  
    136.               
    137.             Intent toIntent = new Intent(CLICK_ITEM_ACTION);  
    138.             PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 200, toIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
    139.             /* 
    140.              * setPendingIntentTemplate 设置pendingIntent 模板 
    141.              * setOnClickFillInIntent   可以将fillInIntent 添加到pendingIntent中 
    142.              */  
    143.             views.setPendingIntentTemplate(visible, pendingIntent);  
    144.               
    145.         } else if (visible == R.id.viewflipper) {  
    146. //          views.setPendingIntentTemplate(R.id.viewflipper, pendingIntentTemplate);  
    147.         }  
    148.           
    149.         AppWidgetManager am = AppWidgetManager.getInstance(context);  
    150.         int[] appWidgetIds = am.getAppWidgetIds(new ComponentName(context, WidgetSetProvider.class));  
    151.         for (int i = 0; i < appWidgetIds.length; i++) {  
    152.             am.updateAppWidget(appWidgetIds[i], views); //更新 实例  
    153.         }  
    154.           
    155.     }  
    156.       
    157. }  
    [java] view plain copy print ?
    1. package com.stone.receiver;  
    2.   
    3. import com.stone.R;  
    4. import com.stone.service.WidgetSetService;  
    5.   
    6. import android.app.PendingIntent;  
    7. import android.appwidget.AppWidgetManager;  
    8. import android.appwidget.AppWidgetProvider;  
    9. import android.content.ComponentName;  
    10. import android.content.Context;  
    11. import android.content.Intent;  
    12. import android.os.Bundle;  
    13. import android.text.TextUtils;  
    14. import android.text.format.DateUtils;  
    15. import android.view.View;  
    16. import android.widget.RemoteViews;  
    17. import android.widget.Toast;  
    18. import android.widget.ViewFlipper;  
    19.   
    20. /** 
    21.  * 使用了集合展示AppWidget 
    22.  * ListView、GridView、StackView 设置adapter,处理item点击 
    23.  * ViewFlipper 在RemoteViews中缺少支持,暂只能在它的布局文件中设置 轮换效果 
    24.  *              对于切换到哪一个子view的item事件不好处理,只能设置一个整体setPendingIntent 
    25.  * @author stone 
    26.  */  
    27. public class WidgetSetProvider extends AppWidgetProvider {  
    28.     public final static String CLICK_ACTION = "com.stone.action.clickset";  
    29.     public final static String CLICK_ITEM_ACTION = "com.stone.action.clickset.item";  
    30.     public final static String EXTRA_ITEM = "extra_item";  
    31.       
    32.     @Override  
    33.     public void onReceive(Context context, Intent intent) {  
    34.         super.onReceive(context, intent);  
    35.         System.out.println(intent.getAction());  
    36.         if (TextUtils.equals(CLICK_ACTION, intent.getAction())) {  
    37.             int extraType = intent.getIntExtra("view_tag", 0);  
    38.             if (extraType > 0) {  
    39.                 System.out.println("extra:::" + extraType);  
    40.                   
    41.                 switch (extraType) {  
    42.                 case 1:  
    43.                     updateWidget(context, R.id.listview, R.id.gridview, R.id.stackview, R.id.viewflipper);  
    44.                     break;  
    45.                 case 2:  
    46.                     updateWidget(context, R.id.gridview, R.id.listview, R.id.stackview, R.id.viewflipper);  
    47.                     break;  
    48.                 case 3:  
    49.                     updateWidget(context, R.id.stackview, R.id.gridview, R.id.listview, R.id.viewflipper);  
    50.                     break;  
    51.                 case 4:  
    52.                     updateWidget(context, R.id.viewflipper, R.id.gridview, R.id.stackview, R.id.listview);  
    53.                     break;  
    54.       
    55.                 default:  
    56.                     break;  
    57.                 }  
    58.             }   
    59.         } else if (TextUtils.equals(CLICK_ITEM_ACTION, intent.getAction())) {  
    60.             Bundle extras = intent.getExtras();  
    61.             int position = extras.getInt(WidgetSetProvider.EXTRA_ITEM, -1);  
    62.             if (position != -1) {  
    63.                 System.out.println("--点击了item---" + position);  
    64.                 System.out.println("");  
    65. //              Toast.makeText(context, "click item:" + position, 0).show();  
    66.             }  
    67.         }  
    68.     }  
    69.   
    70.     @Override  
    71.     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {  
    72.         RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.collections_view_widget);  
    73.           
    74.         Intent intent1 = new Intent(CLICK_ACTION);  
    75.         intent1.putExtra("view_tag", 1);  
    76.         PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 101, intent1, 0);  
    77.         views.setOnClickPendingIntent(R.id.btn_listview, pendingIntent1);  
    78.           
    79.         Intent intent2 = new Intent(CLICK_ACTION);  
    80.         intent2.putExtra("view_tag", 2);  
    81.         PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 102, intent2, 0);  
    82.         views.setOnClickPendingIntent(R.id.btn_gridview, pendingIntent2);  
    83.           
    84.         Intent intent3 = new Intent(CLICK_ACTION);  
    85.         intent3.putExtra("view_tag", 3);  
    86.         PendingIntent pendingIntent3 = PendingIntent.getBroadcast(context, 103, intent3, 0);  
    87.         views.setOnClickPendingIntent(R.id.btn_stackview, pendingIntent3);  
    88.           
    89.         Intent intent4 = new Intent(CLICK_ACTION);  
    90.         intent4.putExtra("view_tag", 4);  
    91.         PendingIntent pendingIntent4 = PendingIntent.getBroadcast(context, 104, intent4, 0);  
    92.         views.setOnClickPendingIntent(R.id.btn_viewflipper, pendingIntent4);  
    93.           
    94.         appWidgetManager.updateAppWidget(appWidgetIds, views);  
    95.           
    96.         System.out.println("setwidget update");  
    97.         super.onUpdate(context, appWidgetManager, appWidgetIds);  
    98.     }  
    99.   
    100.     @Override  
    101.     public void onAppWidgetOptionsChanged(Context context,   
    102.             AppWidgetManager appWidgetManager, int appWidgetId,  
    103.             Bundle newOptions) {  
    104.         super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,  
    105.                 newOptions);  
    106.     }  
    107.   
    108.     @Override  
    109.     public void onDeleted(Context context, int[] appWidgetIds) {  
    110.         super.onDeleted(context, appWidgetIds);  
    111.     }  
    112.   
    113.     @Override  
    114.     public void onEnabled(Context context) {  
    115.         super.onEnabled(context);  
    116.     }  
    117.   
    118.     @Override  
    119.     public void onDisabled(Context context) {  
    120.         super.onDisabled(context);  
    121.     }  
    122.       
    123.     private void updateWidget(Context context, int visible, int gone1, int gone2, int gone3) {  
    124.         //RemoteViews处理异进程中的View  
    125.         RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.collections_view_widget);  
    126.           
    127.         views.setViewVisibility(visible, View.VISIBLE);  
    128.         views.setViewVisibility(gone1, View.GONE);  
    129.         views.setViewVisibility(gone2, View.GONE);  
    130.         views.setViewVisibility(gone3, View.GONE);  
    131.           
    132.         if (visible != R.id.viewflipper) {//viewflipper 不是 继承自AbsListView  or  AdapterViewAnimator  的view  
    133.             Intent intent = new Intent(context, WidgetSetService.class);  
    134.             views.setRemoteAdapter(visible, intent);//设置集合的adapter为intent指定的service  
    135.             views.setEmptyView(visible, R.id.tv_empty);//指定集合view为空时显示的view  
    136.               
    137.             Intent toIntent = new Intent(CLICK_ITEM_ACTION);  
    138.             PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 200, toIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
    139.             /* 
    140.              * setPendingIntentTemplate 设置pendingIntent 模板 
    141.              * setOnClickFillInIntent   可以将fillInIntent 添加到pendingIntent中 
    142.              */  
    143.             views.setPendingIntentTemplate(visible, pendingIntent);  
    144.               
    145.         } else if (visible == R.id.viewflipper) {  
    146. //          views.setPendingIntentTemplate(R.id.viewflipper, pendingIntentTemplate);  
    147.         }  
    148.           
    149.         AppWidgetManager am = AppWidgetManager.getInstance(context);  
    150.         int[] appWidgetIds = am.getAppWidgetIds(new ComponentName(context, WidgetSetProvider.class));  
    151.         for (int i = 0; i < appWidgetIds.length; i++) {  
    152.             am.updateAppWidget(appWidgetIds[i], views); //更新 实例  
    153.         }  
    154.           
    155.     }  
    156.       
    157. }  

    运行的周期函数

    点击stackview的效果图


    你可能感兴趣的:(系统小组件)