android的widget讲解

news/2024/7/4 14:56:59

一、主要框架

  • 1、AppWidgetProvider :继承自 BroadcastRecevier , 在AppWidget 应用 update、enable、disable 和 delete 时接收通知。其中,onUpdate、onReceive 是最常用到的方法,它们接收更新通知。
    • bindAppWidgetId(int appWidgetId, ComponentName provider)
      通过给定的ComponentName 绑定appWidgetId
    • getAppWidgetIds(ComponentName provider)
      通过给定的ComponentName 获取AppWidgetId
    • getAppWidgetInfo(int appWidgetId)
      通过AppWidgetId 获取 AppWidget 信息
    • getInstalledProviders()
      返回一个List<AppWidgetProviderInfo>的信息
    • getInstance(Context context)
      获取 AppWidgetManger 实例使用的上下文对象
    • updateAppWidget(int[] appWidgetIds, RemoteViews views)
      通过appWidgetId 对传进来的 RemoteView 进行修改,并重新刷新AppWidget 组件
    • updateAppWidget(ComponentName provider, RemoteViews views)
      通过 ComponentName 对传进来的 RemoeteView 进行修改,并重新刷新AppWidget 组件
    • updateAppWidget(int appWidgetId, RemoteViews views)
      通过appWidgetId 对传进来的 RemoteView 进行修改,并重新刷新AppWidget 组件
  • 2、 AppWidgetProvderInfo:描述 AppWidget 的大小、更新频率和初始界面等信息,以XML 文件形式存在于应用的 res/xml/目录下。
  • 3、AppWidgetManger :负责管理 AppWidget ,向 AppwidgetProvider 发送通知。
  • 4、RemoteViews :一个可以在其他应用进程中运行的类,向 AppWidgetProvider 发送通知。

    二、开发步骤

    1.在res\xml中建立widget内容提供者文件,取名为widget_provider.xml

    [html] view plain copy print ?
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:minWidth="60dp"  
    4.     android:minHeight="30dp"  
    5.     android:updatePeriodMillis="86400000"       
    6.     android:initialLayout="@layout/main">  
    7. </appwidget-provider>  
    <?xml version="1.0" encoding="UTF-8"?>
    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
        android:minWidth="60dp"
        android:minHeight="30dp"
        android:updatePeriodMillis="86400000"     
        android:initialLayout="@layout/main">
    </appwidget-provider>
    

    android:initialLayout="@layout/main" 此句为指定桌面组件的布局文件。

    android:updatePeriodMillis="86400000"指定刷新时间间隔(单位:ms),每隔android:updatePeriodMillis就调用onUpdate方法。另,如果android:updatePeriodMillis为0,则表示不刷新。

    android:minWidth最小宽度
    android:minHeight最小高度

    可以根据所占单元格数计算android:minWidth和android:minHeight。

    计算公式(宽高皆适用):(单元个数*74)-2。由于像素计算会造成一定的偏差,所以最后值减2。另,屏幕最大单元格数位4*4。

    2.写一个类继承自AppWidgetProvider

    [java] view plain copy print ?
    1. public class widgetProvider extends AppWidgetProvider  
    public class widgetProvider extends AppWidgetProvider


    并重写两个方法

    [java] view plain copy print ?
    1. @Override  
    2.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
    3.             int[] appWidgetIds) {}  
    4.   
    5. @Override  
    6.     public void onReceive(Context context, Intent intent) {}  
    @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                int[] appWidgetIds) {}
    
    @Override
        public void onReceive(Context context, Intent intent) {}

    onUpdate 为组件在桌面上生成时调用,并更新组件UI,onReceiver 为接收广播时调用更新UI,一般这两个方法是比较常用的。

    3.后台注册Receiver

    [html] view plain copy print ?
    1. <receiver android:name=".widgetProvider">  
    2.             <meta-data android:name="android.appwidget.provider"  
    3.                 android:resource="@xml/appwidget_provider"></meta-data>  
    4.             <intent-filter>  
    5.                 <action android:name="com.terry.action.widget.click"></action>  
    6.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />  
    7.                    
    8.             </intent-filter>  
    9.         </receiver>  
    <receiver android:name=".widgetProvider">
                <meta-data android:name="android.appwidget.provider"
                    android:resource="@xml/appwidget_provider"></meta-data>
                <intent-filter>
                    <action android:name="com.terry.action.widget.click"></action>
                    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                     
                </intent-filter>
            </receiver>
    
    

    上面代码中比较重要的是这一句 <meta-data android:name="android.appwidget.provider"  android:resource="@xml/appwidget_provider"></meta-data>  大意为指定桌面应用程序的AppWidgetProvderInfo  文件,使其可作其管理文件。

    4.使app widget组件支持点击事件

    [java] view plain copy print ?
    1. public static void updateAppWidget(Context context,  
    2.             AppWidgetManager appWidgeManger, int appWidgetId) {  
    3.         rv = new RemoteViews(context.getPackageName(), R.layout.main);  
    4.         Intent intentClick = new Intent(CLICK_NAME_ACTION);  
    5.         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,  
    6.                 intentClick, 0);  
    7.         rv.setOnClickPendingIntent(R.id.TextView01, pendingIntent);  
    8.         appWidgeManger.updateAppWidget(appWidgetId, rv);  
    9.     }  
    public static void updateAppWidget(Context context,
                AppWidgetManager appWidgeManger, int appWidgetId) {
            rv = new RemoteViews(context.getPackageName(), R.layout.main);
            Intent intentClick = new Intent(CLICK_NAME_ACTION);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                    intentClick, 0);
            rv.setOnClickPendingIntent(R.id.TextView01, pendingIntent);
            appWidgeManger.updateAppWidget(appWidgetId, rv);
        }
    

    此方法为创建组件时 onUpdate 调用的更新UI的方法,代码中使用RemoteView 找到组件的布局文件,同时为其设置广播接收器CLICK_NAME_ACTION并且通过RemoteView 的setOnClickPendingIntent 方法找到我想触发事件的TextView 为其设置广播。接着在onReceiver 中通过判断传进来的广播来触发动作。

    [java] view plain copy print ?
    1. @Override  
    2.     public void onReceive(Context context, Intent intent) {  
    3.         // TODO Auto-generated method stub   
    4.         super.onReceive(context, intent);  
    5.   
    6.         if (rv == null) {  
    7.             rv = new RemoteViews(context.getPackageName(), R.layout.main);  
    8.         }  
    9.         if (intent.getAction().equals(CLICK_NAME_ACTION)) {  
    10.             if (uitil.isChange) {  
    11.                 rv.setTextViewText(R.id.TextView01, context.getResources()  
    12.                         .getString(R.string.load));  
    13.   
    14.             } else {  
    15.                 rv.setTextViewText(R.id.TextView01, context.getResources()  
    16.                         .getString(R.string.change));  
    17.   
    18.             }  
    19.             Toast.makeText(context, Boolean.toString(uitil.isChange),  
    20.                     Toast.LENGTH_LONG).show();  
    21.             uitil.isChange = !uitil.isChange;  
    22.   
    23.         }  
    24.         AppWidgetManager appWidgetManger = AppWidgetManager  
    25.                 .getInstance(context);  
    26.         int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName(  
    27.                 context, widgetProvider.class));  
    28.         appWidgetManger.updateAppWidget(appIds, rv);  
    29.     } 
    原网址: http://blog.csdn.net/zyting_love/article/details/8115493

http://www.niftyadmin.cn/n/3649509.html

相关文章

非托管对象 和托管对象_如何使用托管数据库和对象存储设置可扩展的Laravel 6应用程序

非托管对象 和托管对象介绍 (Introduction) When scaling web applications horizontally, the first difficulties you’ll typically face are dealing with file storage and data persistence. This is mainly due to the fact that it is hard to maintain consistency of…

微笑涛声博客正式关联第三方博客平台

第三方博客平台简介 第三方博客指的是不要求自己有域名&#xff0c;空间&#xff0c;服务器&#xff0c;仅在大型门户网址注册就可运行的博客平台。 这类博客有新浪&#xff0c;搜狐&#xff0c;和讯&#xff0c;网易等。第三方博客现在已经成为更多网络爱好者发布自己心情&…

[Domino]Java访问Domino邮件代码片断[1]

[Domino]Java访问Domino邮件代码片断编写者日期关键词郑昀ultrapower2005-6-20Java Domino得到用户收件箱中的邮件三个知识点&#xff1a;1&#xff1a;如果是打开mailfile数据库后直接Database dbMail sNotes.getDatabase(sNotes.getServerName(), mailfile, false);Document…

sidekiq redis_如何将Sidekiq和Redis添加到Ruby on Rails应用程序

sidekiq redis介绍 (Introduction) When developing a Ruby on Rails application, you may find you have application tasks that should be performed asynchronously. Processing data, sending batch emails, or interacting with external APIs are all examples of work…

使用Axure RP进行博客系统的原型设计

原型设计 我使用Axure RP软件对个人博客系统进行原型设计&#xff0c;其实就是仿照我的博客进行简单的设计&#xff0c;然后做一些改变。以下是博客首页的原型设计图。虽然很丑&#xff0c;但是是第一次接触原型设计。再接再厉。

Eclipse特色主题推荐——Marketplace

eclipse皮肤 由于最近Android开发使用Eclipse&#xff0c;所以推荐一款Eclipse的主题。前面习惯了Android studio的代码风格&#xff0c;虽然eclipse自带有几款不错的主题&#xff0c;不过优化的不是很好。在网上发现了一款主题&#xff0c;非常不错&#xff0c;安装也比较简单…

人工智能(pytorch)搭建模型17-pytorch搭建ReitnNet模型,加载数据进行模型训练与预测

大家好&#xff0c;我是微学AI&#xff0c;今天给大家介绍一下人工智能(pytorch)搭建模型17-pytorch搭建ReitnNet模型&#xff0c;加载数据进行模型训练与预测&#xff0c;RetinaNet 是一种用于目标检测任务的深度学习模型&#xff0c;旨在解决目标检测中存在的困难样本和不平衡…