系统小组件

2015-07-22  本文已影响126人  4528283108ee

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 plaincopyprint?
    1. <receiver android:name="com.stone.receiver.WidgetSetProvider">  
    2.              <intent-filter>  
    3.                  <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>  
    4.                  <action android:name="com.stone.action.clickset"/>  
    5.                  <action android:name="com.stone.action.clickset.item"/>  
    6.              </intent-filter>  
    7.              <meta-data android:name="android.appwidget.provider"  
    8.                  android:resource="@xml/set_widget_provider"/>  
    9. </receiver>  
    [html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    1. <receiver android:name="com.stone.receiver.WidgetSetProvider">  
    2.              <intent-filter>  
    3.                  <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>  
    4.                  <action android:name="com.stone.action.clickset"/>  
    5.                  <action android:name="com.stone.action.clickset.item"/>  
    6.              </intent-filter>  
    7.              <meta-data android:name="android.appwidget.provider"  
    8.                  android:resource="@xml/set_widget_provider"/>  
    9. </receiver>  

    res/xml/set_widget_provider.xml

    [html] view plaincopyprint?
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:minWidth="250dp"  
    4.     android:minHeight="180dp"  
    5.     android:updatePeriodMillis="5000"  
    6.     android:previewImage="@drawable/ic_launcher"  
    7.     android:initialLayout="@layout/collections_view_widget"  
    8.     android:resizeMode="horizontal|vertical"  
    9.     android:autoAdvanceViewId="@id/viewflipper"  >  
    10.     <!--   
    11.         计算size的公式: (70*n) -30  n为部件所需的大小(占几格)   当前的就是  4x4  
    12.         minResizeWidth  
    13.         minResizeHeight   能被调整的最小宽高,若大于minWidth minHeight 则忽略  
    14.         label   选择部件时看到标签  
    15.         icon    选择部件时看到图标  
    16.         updatePeriodMillis  更新时间间隔  
    17.         previewImage    选择部件时 展示的图像  3.0以上使用  
    18.         initialLayout   布局文件  
    19.         resizeMode      调整size模式  
    20.         configure       如果需要在启动前先启动一个Activity进行设置,在这里给出Activity的完整类名  
    21.         autoAdvanceViewId=@id/xx    与集合部件一起使用,指定该集合item自动推进 暂只发现对stackview有效,会自动一段时间推进到下一个  
    22.           
    23.         集合部件:3.0后才有。set view:ListView、GridView、StackView、AdapterViewFlipper  
    24.         ViewFlipper 为非集合部件  
    25.      -->  
    26. </appwidget-provider>  
    [html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:minWidth="250dp"  
    4.     android:minHeight="180dp"  
    5.     android:updatePeriodMillis="5000"  
    6.     android:previewImage="@drawable/ic_launcher"  
    7.     android:initialLayout="@layout/collections_view_widget"  
    8.     android:resizeMode="horizontal|vertical"  
    9.     android:autoAdvanceViewId="@id/viewflipper"  >  
    10.     <!--   
    11.         计算size的公式: (70*n) -30  n为部件所需的大小(占几格)   当前的就是  4x4  
    12.         minResizeWidth  
    13.         minResizeHeight   能被调整的最小宽高,若大于minWidth minHeight 则忽略  
    14.         label   选择部件时看到标签  
    15.         icon    选择部件时看到图标  
    16.         updatePeriodMillis  更新时间间隔  
    17.         previewImage    选择部件时 展示的图像  3.0以上使用  
    18.         initialLayout   布局文件  
    19.         resizeMode      调整size模式  
    20.         configure       如果需要在启动前先启动一个Activity进行设置,在这里给出Activity的完整类名  
    21.         autoAdvanceViewId=@id/xx    与集合部件一起使用,指定该集合item自动推进 暂只发现对stackview有效,会自动一段时间推进到下一个  
    22.           
    23.         集合部件:3.0后才有。set view:ListView、GridView、StackView、AdapterViewFlipper  
    24.         ViewFlipper 为非集合部件  
    25.      -->  
    26. </appwidget-provider>  

    layout/collections_view_widget.xml

    [html] view plaincopyprint?
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="match_parent"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <LinearLayout  
    8.         android:layout_width="fill_parent"  
    9.         android:layout_height="wrap_content" >  
    10.   
    11.         <Button  
    12.             android:id="@+id/btn_listview"  
    13.             android:layout_width="0dp"  
    14.             android:layout_height="wrap_content"  
    15.             android:layout_weight="1"  
    16.             android:text="listview" />  
    17.   
    18.         <Button  
    19.             android:id="@+id/btn_gridview"  
    20.             android:layout_width="0dp"  
    21.             android:layout_height="wrap_content"  
    22.             android:layout_weight="1"  
    23.             android:text="gridview" />  
    24.   
    25.         <Button  
    26.             android:id="@+id/btn_stackview"  
    27.             android:layout_width="0dp"  
    28.             android:layout_height="wrap_content"  
    29.             android:layout_weight="1"  
    30.             android:text="stackview" />  
    31.   
    32.         <Button  
    33.             android:id="@+id/btn_viewflipper"  
    34.             android:layout_width="0dp"  
    35.             android:layout_height="wrap_content"  
    36.             android:layout_weight="1"  
    37.             android:text="viewflipper" />  
    38.     </LinearLayout>  
    39.   
    40.     <FrameLayout  
    41.         xmlns:android="http://schemas.android.com/apk/res/android"  
    42.         android:layout_width="match_parent"  
    43.         android:layout_height="match_parent"  
    44.         android:background="#80000000" >  
    45.         />  
    46.   
    47.         <ListView  
    48.             android:id="@+id/listview"  
    49.             android:layout_width="fill_parent"  
    50.             android:layout_height="fill_parent" />  
    51.   
    52.         <GridView  
    53.             android:id="@+id/gridview"  
    54.             android:layout_width="fill_parent"  
    55.             android:layout_height="fill_parent"  
    56.             android:numColumns="2"  
    57.             android:visibility="gone" />  
    58.   
    59.         <StackView  
    60.             android:id="@+id/stackview"  
    61.             android:layout_width="fill_parent"  
    62.             android:layout_height="fill_parent"  
    63.             android:visibility="gone" />  
    64.   
    65.         <ViewFlipper  
    66.             android:id="@+id/viewflipper"  
    67.             android:layout_width="fill_parent"  
    68.             android:layout_height="fill_parent"  
    69.             android:autoStart="true"  
    70.             android:flipInterval="2000"  
    71.             android:visibility="gone" >  
    72. <!--   
    73. autoStart=true  <==>  startFlipping()  
    74. flipInterval=2000 <==> How long to wait before flipping to the next view  
    75.  -->  
    76.             <ImageView  
    77.                 android:id="@+id/iv1"  
    78.                 android:layout_width="fill_parent"  
    79.                 android:layout_height="fill_parent"  
    80.                 android:background="@drawable/a11"/>  
    81.             <ImageView  
    82.                 android:id="@+id/iv2"  
    83.                 android:layout_width="fill_parent"  
    84.                 android:layout_height="fill_parent"  
    85.                 android:background="@drawable/a2"/>  
    86.             <ImageView  
    87.                 android:id="@+id/iv3"  
    88.                 android:layout_width="fill_parent"  
    89.                 android:layout_height="fill_parent"  
    90.                 android:background="@drawable/a3" />  
    91.             <ImageView  
    92.                 android:id="@+id/iv4"  
    93.                 android:layout_width="fill_parent"  
    94.                 android:layout_height="fill_parent"  
    95.                 android:background="@drawable/a4" />  
    96.         </ViewFlipper>  
    97.   
    98.         <TextView  
    99.             android:id="@+id/tv_empty"  
    100.             android:layout_width="fill_parent"  
    101.             android:layout_height="fill_parent"  
    102.             android:gravity="center"  
    103.             android:text="Empty List"  
    104.             android:visibility="gone" />  
    105.     </FrameLayout>  
    106.   
    107. </LinearLayout>  
    [html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="match_parent"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <LinearLayout  
    8.         android:layout_width="fill_parent"  
    9.         android:layout_height="wrap_content" >  
    10.   
    11.         <Button  
    12.             android:id="@+id/btn_listview"  
    13.             android:layout_width="0dp"  
    14.             android:layout_height="wrap_content"  
    15.             android:layout_weight="1"  
    16.             android:text="listview" />  
    17.   
    18.         <Button  
    19.             android:id="@+id/btn_gridview"  
    20.             android:layout_width="0dp"  
    21.             android:layout_height="wrap_content"  
    22.             android:layout_weight="1"  
    23.             android:text="gridview" />  
    24.   
    25.         <Button  
    26.             android:id="@+id/btn_stackview"  
    27.             android:layout_width="0dp"  
    28.             android:layout_height="wrap_content"  
    29.             android:layout_weight="1"  
    30.             android:text="stackview" />  
    31.   
    32.         <Button  
    33.             android:id="@+id/btn_viewflipper"  
    34.             android:layout_width="0dp"  
    35.             android:layout_height="wrap_content"  
    36.             android:layout_weight="1"  
    37.             android:text="viewflipper" />  
    38.     </LinearLayout>  
    39.   
    40.     <FrameLayout  
    41.         xmlns:android="http://schemas.android.com/apk/res/android"  
    42.         android:layout_width="match_parent"  
    43.         android:layout_height="match_parent"  
    44.         android:background="#80000000" >  
    45.         />  
    46.   
    47.         <ListView  
    48.             android:id="@+id/listview"  
    49.             android:layout_width="fill_parent"  
    50.             android:layout_height="fill_parent" />  
    51.   
    52.         <GridView  
    53.             android:id="@+id/gridview"  
    54.             android:layout_width="fill_parent"  
    55.             android:layout_height="fill_parent"  
    56.             android:numColumns="2"  
    57.             android:visibility="gone" />  
    58.   
    59.         <StackView  
    60.             android:id="@+id/stackview"  
    61.             android:layout_width="fill_parent"  
    62.             android:layout_height="fill_parent"  
    63.             android:visibility="gone" />  
    64.   
    65.         <ViewFlipper  
    66.             android:id="@+id/viewflipper"  
    67.             android:layout_width="fill_parent"  
    68.             android:layout_height="fill_parent"  
    69.             android:autoStart="true"  
    70.             android:flipInterval="2000"  
    71.             android:visibility="gone" >  
    72. <!--   
    73. autoStart=true  <==>  startFlipping()  
    74. flipInterval=2000 <==> How long to wait before flipping to the next view  
    75.  -->  
    76.             <ImageView  
    77.                 android:id="@+id/iv1"  
    78.                 android:layout_width="fill_parent"  
    79.                 android:layout_height="fill_parent"  
    80.                 android:background="@drawable/a11"/>  
    81.             <ImageView  
    82.                 android:id="@+id/iv2"  
    83.                 android:layout_width="fill_parent"  
    84.                 android:layout_height="fill_parent"  
    85.                 android:background="@drawable/a2"/>  
    86.             <ImageView  
    87.                 android:id="@+id/iv3"  
    88.                 android:layout_width="fill_parent"  
    89.                 android:layout_height="fill_parent"  
    90.                 android:background="@drawable/a3" />  
    91.             <ImageView  
    92.                 android:id="@+id/iv4"  
    93.                 android:layout_width="fill_parent"  
    94.                 android:layout_height="fill_parent"  
    95.                 android:background="@drawable/a4" />  
    96.         </ViewFlipper>  
    97.   
    98.         <TextView  
    99.             android:id="@+id/tv_empty"  
    100.             android:layout_width="fill_parent"  
    101.             android:layout_height="fill_parent"  
    102.             android:gravity="center"  
    103.             android:text="Empty List"  
    104.             android:visibility="gone" />  
    105.     </FrameLayout>  
    106.   
    107. </LinearLayout>  


    集合的数据源 需要 继承 RemoteViewsService [java] view plaincopyprint?
    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<String> mWidgetItems = new ArrayList<String>();  
    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 plaincopyprint?在CODE上查看代码片派生到我的代码片
    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<String> mWidgetItems = new ArrayList<String>();  
    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 plaincopyprint?
    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 plaincopyprint?在CODE上查看代码片派生到我的代码片
    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的效果图


    上一篇 下一篇

    猜你喜欢

    热点阅读