Android中经常用到APP Widget,如时钟,天气预报等。

长按屏幕,在弹出的对话框中选择“窗口小部件”,然后就列出了可选择的小部件,这些小部件就是APP Widget。

本文开发一个APP Widget,在屏幕上显示当前的时间,并且每秒更新一次。

开发APP Widget需要以下三个xml文件。

(1)AndroidManifest.xml,这个是所有APP都有的文件,APP Widget的AndroidManifest.xml和其他的AndroidManifest.xml有所不同。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.hzhi.time_widget"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6.  
  7. <uses-sdk
  8. android:minSdkVersion="8"
  9. android:targetSdkVersion="17" />
  10.  
  11. <application
  12. android:allowBackup="true"
  13. android:icon="@drawable/ic_launcher"
  14. android:label="@string/app_name"
  15. android:theme="@style/AppTheme" >
  16.  
  17. <receiver android:name="MyTime" android:label="MyTime">
  18. <intent-filter>
  19. <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  20. </intent-filter>
  21. <meta-data android:name="android.appwidget.provider" android:resource="@xml/my_time" />
  22. </receiver>
  23.  
  24. <service android:name="MyTime$MyService" />
  25.  
  26. </application>
  27.  
  28. </manifest>

其中,一个Receiver就代表一个APP Widget,如果想在一个工程里面开发多个APP Widget,多写几个Receiver就可以。<receiver android:name="MyTime" android:label="MyTime">表明该APP Widget的名称和标签。

<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />表明接收APPWIDGET_UPDATE,这是必须有的,否则APP Widget无法更新。

<meta-data android:name="android.appwidget.provider" android:resource="@xml/my_time" />指明了APP Widget的信息文件为@xml/my_time,这也是下面将介绍的。

(2)APP Widget的信息文件,该文件是APP Widget所特有的。在res文件夹下面新建一个xml文件夹,在里面新建一个my_time.xml文件,即是APP Widget的信息文件。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appwidget-provider
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:minWidth="220dip"
  5. android:minHeight="146dip"
  6. android:updatePeriodMillis="0"
  7. android:initialLayout="@layout/my_time"
  8. />

该文件设置了APP Widget的长和宽(长和宽的值应为74*n-2),以及更新间隔时间(android:updatePeriodMillis,本例使用Timer更新,所以设置为0),最后的android:initialLayout="@layout/my_time" 指明了APP Widget的布局文件,也就是显示在桌面上的布局。

(3)APP Widget的布局文件,本例为my_time.xml,基本和其他APP的布局文件一样,区别就在于整个布局文件只有一个TextView。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/TextView01"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:gravity="center"
  7. android:text="Starting..."
  8. android:textColor="#FFFF00"
  9. android:textSize="20pt" />

最后是java文件MyTime.java:

  1. package com.hzhi.time_widget;
  2.  
  3. import java.util.Date;
  4. import java.util.Timer;
  5. import java.util.TimerTask;
  6.  
  7. import android.os.Bundle;
  8. import android.os.Handler;
  9. import android.os.IBinder;
  10. import android.os.Message;
  11. import android.app.Activity;
  12. import android.app.Service;
  13. import android.appwidget.AppWidgetManager;
  14. import android.appwidget.AppWidgetProvider;
  15. import android.content.ComponentName;
  16. import android.content.Context;
  17. import android.content.Intent;
  18. import android.util.Log;
  19. import android.view.Menu;
  20. import android.widget.RemoteViews;
  21. import android.widget.Toast;
  22.  
  23. public class MyTime extends AppWidgetProvider {
  24.  
  25. Timer timer;
  26. Context context;
  27.  
  28. //onUpdate
  29. @Override
  30. public void onUpdate(Context con, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  31. context = con;
  32. Intent intent = new Intent(context, MyService.class);
  33. timer = new Timer();
  34. timer.schedule(timertask, 0, 1000);
  35. }
  36. //MyService服务程序
  37. public static class MyService extends Service {
  38. @Override
  39. public void onStart(Intent intent, int startId) {
  40. RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_time);
  41. remoteViews.setTextViewText(R.id.TextView01, new Date().toLocaleString());
  42. ComponentName thisWidget = new ComponentName(this, MyTime.class);
  43. AppWidgetManager manager = AppWidgetManager.getInstance(this);
  44. manager.updateAppWidget(thisWidget, remoteViews);
  45. }
  46. @Override
  47. public IBinder onBind(Intent intent) {
  48. return null;
  49. }
  50. };
  51.  
  52. // Handler
  53. private Handler handler = new Handler(){
  54. public void handleMessage(Message msg){
  55. Intent intent = new Intent(context, MyService.class);
  56. context.startService(intent);
  57. }
  58. };
  59.  
  60. private TimerTask timertask = new TimerTask(){
  61. public void run(){
  62. Message message = new Message();
  63. handler.sendMessage(message);
  64. }
  65. };
  66.  
  67. }

该Java文件使用Timer,每秒钟开始一次服务MyService。

MyService获得当前的时间,并且以AppWidgetManager.updateAppWidget()方法更新APP Widget所显示的时间。

运行APP,并添加到桌面上。

APP Widget的开发的更多相关文章

  1. [安卓开发]App Widget开发入门指导

    本节所要讲的主要内容包括Android桌面小部件.App Widget的开发入门指导,并通过一个简单实例的形式来直观的讲解App Widget. 一.Widget .App Widget .Web A ...

  2. Android开发之创建App Widget和更新Widget内容

    App WidgetsApp Widgets are miniature application views that can be embedded in other applications (s ...

  3. App Widget简单应用

    首先后台进程创建一个PendingIntent对象,其中PendingIntent中包含一个真正的Intent,创建完成后将此PendingIntent对象交给桌面控件所在的进程,当用户点击桌面控件或 ...

  4. App Widget

    AppWidgetProviderInfo对象: 为App Widget提供元数据(描述数据的数据,如XML.关系型数据的表结构),包括布 局,更新频率等数据.这个对象被定义在XML文件当中: App ...

  5. 一个App Widget实例第一次创建时被调用

    事实上已经有很多的所谓的路由框架做到这一点,我也没有去研究别的,加上一直对backbone这个框架的评价不错,所以就琢磨着怎么用它实现我所需要的SPA的url管理了. 比如,你可能会说"如果 ...

  6. 实战使用Axure设计App,使用WebStorm开发(6) – 迈向后端

    系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求  实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目   实战使 ...

  7. 实战使用Axure设计App,使用WebStorm开发(5) – 实现页面功能

    系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求  实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目   实战使 ...

  8. 实战使用Axure设计App,使用WebStorm开发(4) – 实现页面UI

    系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求  实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目   实战使 ...

  9. 实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目

    系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求  实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目   实战使 ...

随机推荐

  1. Atitit 作用域的理解attilax总结

    Atitit 作用域的理解attilax总结 1.1. 作用域是指对某一变量和方法具有访问权限的代码空间, 1 1.2. 作用域的使用提高了程序逻辑的局部性,增强程序的可靠性,减少名字冲突.1 1.3 ...

  2. instanceof, typeof, & Object.prototype.toString

    /** * * @authors Your Name (you@example.org) * @date 2016-11-18 09:31:23 * @version $Id$ */instanceo ...

  3. jeasyUI的treegrid批量删除多行(转载)

    看上去,JavaScript的变量类型,也可以分为值类型和引用类型.赋值操作中,值类型,各自独立,互不干涉:引用类型,指针而已,大家指向同一个对象. 为什么这样说呢? 我是从jeasyUI的treeg ...

  4. POJ3069 POJ2586 解题报告(异曲同工的贪心算法)

    [POJ 3069](2586见下) 原题在此:http://poj.org/problem?id=3069 题目大意: 一个直线上有N个点.点i的距离是Xi.从这些点中选取若干个加上标记.要求:对于 ...

  5. python导入模块和包的使用

    做项目的时候经常会要求做到模块化管理,即同样功能的代码放到同一个文件夹下,以便于方便管理,相信很多人对模块的引用都模糊不清,今天鄙人在这里就总结下. 一.模块导入的使用 在同一个文件夹下有两个文件分别 ...

  6. wamp2.5 配置Apache允许外网访问

    找到<Directory "e:/wamp/www/">节点,在里面添加Require all granted

  7. javscript对cookie的操作,以及封装

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. php基础教程-数据类型

    PHP 支持八种原始类型(type). 四种标量类型: string(字符串) integer(整型) float(浮点型,也作 double ) boolean(布尔型) 两种复合类型: array ...

  9. 转载-centos网络配置(手动设置,自动获取)的2种方法

    转载地址:http://blog.51yip.com/linux/1120.html 重新启动网络配置 # service network restart 或 # /etc/init.d/networ ...

  10. 材价看板(1)- 如何建立你的第一个kanban,看看这些暴露的问题你们有没有?

    今年负责一个老产品新团队,和几年前的指标组一样,现在的团队没有采用什么研发方法,于是我开始了团队的看板之旅. 12月22日给材价整个部门的产品研发相关人员做了一次kanban工作坊培训,    以及敏 ...