android桌面组件开发

news/2024/7/7 8:09:48

      Widget是Android1.5版所引进的特性之一.Widget,可让用户在主屏幕界面及时了解程序显示的重要信息.标准的Android系统已包含几个Widget的示例,如模拟时钟,音乐播放器等.

1、AppWidget 框架类

  • 1、AppWidgetProvider :继承自 BroadcastRecevier , 在AppWidget 应用 update、enable、disable 和 delete 时接收通知。其中,onUpdate、onReceive 是最常用到的方法,它们接收更新通知。
  • 2、 AppWidgetProvderInfo:描述 AppWidget 的大小、更新频率和初始界面等信息,以XML 文件形式存在于应用的 res/xml/目录下。
  • 3、AppWidgetManger :负责管理 AppWidget ,向 AppwidgetProvider 发送通知。
  • 4、RemoteViews :一个可以在其他应用进程中运行的类,向 AppWidgetProvider 发送通知。

 2、AppWidget 框架的主要类介绍

 1) AppWidgetManger 类

  • 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) 继承自 AppWidgetProvider 可实现的方法为如下:

  • 1、onDeleted(Context context, int[] appWidgetIds)
  • 2、onDisabled(Context context)
  • 3、onEnabled(Context context)
  • 4、onReceive(Context context, Intent intent)
      Tip:因为 AppWidgetProvider 是继承自BroadcastReceiver  所以可以重写onRecevie 方法,当然必须在后台注册Receiver
  • 5、onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)

3、Demo讲解

1.建立Widget内容提供者文件,我们在res下建立xml文件夹,并且新建一个widget_provider.xml代码入下:

复制代码
 
  
<? xml version = " 1.0 "  encoding = " utf-8 " ?>  < appwidget - provider xmlns:android = " http://schemas.android.com/apk/res/android "  android:minWidth = " 50dip "  android:minHeight = " 50dip "  android:updatePeriodMillis = " 10000 "  android:initialLayout = " @layout/main "  />

Tip:上文说过AppWidgetProvderInfo 是在res/xml 的文件形式存在的,看参数不难理解,比较重要的是这里android:initialLayout="@layout/main" 此句为指定桌面组件的布局文件。

主要设置的参数如下:minWidth: 定义Wdiget组件的宽度minHeight: 定义Wdiget组件的高度updatePeriodMillis: 更新的时间周期initialLayout: Widget的布局文件configure: 如果需要在启动前先启动一个Activity进行设置,在这里给出Activity的完整类名(后面会说到,与一般Activity的实现有些许差别)*Widget大小的计算 :(单元格数*74)-2,API上说是为了防止像素计算时的整数舍入导致错所以-2...不是很明白 

2.修改main.xml布局,代码如下:

 
 
<? xml version = " 1.0 "  encoding = " utf-8 " ?> 
< LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android " 
android:orientation
= " vertical " 
android:layout_width
= " fill_parent " 
android:layout_height
= " fill_parent " 
android:background
= " @drawable/wordcup " 
> 
< TextView
android:id
= " @+id/wordcup " 
android:layout_width
= " fill_parent " 
android:layout_height
= " wrap_content " 
android:text
= " @string/hello " 
android:textSize
= " 12px " 
android:textColor
= " #ff0000 " 
/>
</ LinearLayout >

Tips:定义了Widget界面布局的XML文件(位于res/layout/..),需要注意的是使用的组件必须是RemoteViews所支持的,目前原生API中支持的组件如下:

FrameLayout、LinearLayout、RelativeLayout

AnalogClock、Button、Chronmeter、ImageButton、ImageView、ProgressBar、TextView
 

*如果使用了除此之外的组件,则在Widget创建时会导致android.view.InflateExceptionn异常。

PS:这就导致有一些功能或样式无法实现,如很基本的list或文本编辑框都是无法直接实现的。如果想自定义Widget中的View的话只能通过修改framework来提供相应组件的支持。

3.写一个类继承自AppWidgetProvider

 
 
package  com.android.tutor; import  java.util.Calendar; import  java.util.Date; import  java.util.GregorianCalendar; import  java.util.Timer; import  java.util.TimerTask; import  android.appwidget.AppWidgetManager; import  android.appwidget.AppWidgetProvider; import  android.content.ComponentName; import  android.content.Context; import  android.widget.RemoteViews; public  class  WidetDemo  extends  AppWidgetProvider { /**  Called when the activity is first created.  */  @Override public  void  onUpdate(Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds) {Timer timer  =  new  Timer();timer.scheduleAtFixedRate( new  MyTime(context,appWidgetManager),  1 ,  60000 ); super .onUpdate(context, appWidgetManager, appWidgetIds);} private  class  MyTime  extends  TimerTask{RemoteViews remoteViews;AppWidgetManager appWidgetManager;ComponentName thisWidget; public  MyTime(Context context,AppWidgetManager appWidgetManager){ this .appWidgetManager  =  appWidgetManager;remoteViews  =  new  RemoteViews(context.getPackageName(),R.layout.main);thisWidget  =  new  ComponentName(context,WidetDemo. class );} public  void  run() {Date date  =  new  Date();Calendar calendar  =  new  GregorianCalendar( 2010 , 06 , 11 ); long  days  =  (((calendar.getTimeInMillis() - date.getTime()) / 1000 )) / 86400 ;remoteViews.setTextViewText(R.id.wordcup,  " 距离南非世界杯还有 "  +  days + " " );appWidgetManager.updateAppWidget(thisWidget, remoteViews);}}}

Tips:

AppWidgetProvider实际上就是一个BroadcastReceiver,里面提供了以下函数:onReceive(Context, Intent)onUpdate(Context , AppWidgetManager, int[] appWidgetIds)onEnabled(Context)onDeleted(Context, int[] appWidgetIds)onDisabled(Context)可通过重写以上函数来监听Widget状态的变化并进行相应的处理。

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

Widget的更新与Activity不同,必须借助于RemoteViews和AppWidgetMananger。

 
 
[启动  -  无confiure Activity]
onReceive
onEnabled —— 第一个widget被显示
onReceive
onUpdate —— 刷新界面

[启动
  -  带confiuration Activity]
onReceive
onUpdate

[拖动]
< 无状态变化 >

[周期更新]
onReceive
onUpdate

[删除]
onReceive
onDeleted —— widget被删除
onReceive
onDisabled —— 最后一个widget被移除

[启动时位置不够]
onReceive
onEnabled
onReceive
onUpdate
onReceive
onDeleted
onReceive
onDisabled

*每次状态的变化会触发onReceive,一般该函数是不需要重写的。 

4.修改配置文件AndroidManifest.xml,后台注册Receiver,代码如下:

 
 
<? xml version = " 1.0 "  encoding = " utf-8 " ?>  < manifest xmlns:android = " http://schemas.android.com/apk/res/android "  package = " com.android.tutor "  android:versionCode = " 1 "  android:versionName = " 1.0 " >  < application android:icon = " @drawable/icon "  android:label = " @string/app_name " >  < receiver android:name = " .WidetDemo "  android:label = " @string/app_name " >  < intent - filter >  < action android:name = " android.appwidget.action.APPWIDGET_UPDATE "  />  </ intent - filter >  < meta - data android:name = " android.appwidget.provider "  android:resource = " @xml/widget_provider "  />  </ receiver >  </ application >  < uses - sdk android:minSdkVersion = " 7 "  />  </ manifest >

Tips:

因为是桌面组件,所以暂时不考虑使用Activity 界面,当然你在实现做项目时可能会需要点击时跳转到Activity 应用程序上做操作,典型的案例为Android  提供的音乐播放器。

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

5.添加修改资源

增加图片素材。

< resources >   < string name = " hello " > Hello World, WidetDemo !</ string >   < string name = " app_name " > DaysToWorldCup </ string >   </ resources >

增加图片素材。

 
 
<? xml version = " 1.0 "  encoding = " utf-8 " ?> 
< resources > 
< string name = " hello " > Hello World, WidetDemo !</ string > 
< string name = " app_name " > DaysToWorldCup </ string > 
</ resources >




转载网址:http://blog.csdn.net/robertcpp/article/details/18596213



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

相关文章

网页开端第三次培训笔记

CSS主要内容 1.CSS是一种用来表现HTML或XML等文件样式的计算机语言&#xff0c;是用来美化页面用的&#xff0c;没有网页 则CSS毫无用处&#xff0c;所以CSS需要依赖HTML展示其功能。 CSS 2.CSS基本语法 css样式由选择器和一条或多条以分号隔开的样式声明组成&#xff0c; …

流式布局的使用——使用固定布局

首先可以看下效果图 简单布局使用: <com.hbwj.p2pinvest.ui.FlowLayoutandroid:id"id/flow_hot"android:layout_width"match_parent"android:layout_height"wrap_content"android:background"android:color/holo_blue_light">&l…

Android中关于横竖屏问题

在以前的版本中只要在AndroidManifest.xml文件中对activity指定android:configChanges"keyboardHidden|orientation"属性&#xff0c;转屏的时候就会不再重新调用OnCreate()函数&#xff0c;而是调用onConfigurationChanged()。 但是在自从android3.2以后&#xff0c…

应用双节点,数据库是dg_如何使用节点构建轻量级发票应用程序:数据库和API

应用双节点,数据库是dg介绍 (Introduction) To get paid for goods and services provided, businesses need to send invoices to their customers informing them of the services that they will be charged for. Back then, people had paper invoices which they gave to …

查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)

//search.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "LinkTable.h"#defineMAX_KEY 500//------------------------------数组实现部分----------------------------------/**//* 无序数组顺序查找算法…

网页开端第四次培训笔记

CSS常用属性设置 1.背景&#xff08;css背景属性用于定义HTML元素的背景效果 background-color 设置元素的背景效果background-image 设置元素的背景图像&#xff0c;默认情况下&#xff0c;背景图像进行平铺重复显示&#xff0c; …

父子节点 构建_如何使用节点构建轻量级发票应用程序:用户界面

父子节点 构建介绍 (Introduction) In the first part of this series, you set up the backend server for the invoicing application. In this tutorial you will build the part of the application that users will interact with, known as the user interface. 在本系列…

Android零碎小知识

获取当前的版本号 public double getVersionCode() {try {int versionCode getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;return versionCode;} catch (PackageManager.NameNotFoundException e) {e.printStackTrace();}return 0; }将版本号设置成1.…