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

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

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

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

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hzhi.time_widget"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" > <receiver android:name="MyTime" android:label="MyTime">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/my_time" />
</receiver> <service android:name="MyTime$MyService" /> </application> </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的信息文件。

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

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

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

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

最后是java文件MyTime.java:

package com.hzhi.time_widget;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask; import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.app.Activity;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.RemoteViews;
import android.widget.Toast; public class MyTime extends AppWidgetProvider { Timer timer;
Context context; //onUpdate
@Override
public void onUpdate(Context con, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
context = con;
Intent intent = new Intent(context, MyService.class);
timer = new Timer();
timer.schedule(timertask, 0, 1000);
}
//MyService服务程序
public static class MyService extends Service {
@Override
public void onStart(Intent intent, int startId) {
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_time);
remoteViews.setTextViewText(R.id.TextView01, new Date().toLocaleString());
ComponentName thisWidget = new ComponentName(this, MyTime.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, remoteViews);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}; // Handler
private Handler handler = new Handler(){
public void handleMessage(Message msg){
Intent intent = new Intent(context, MyService.class);
context.startService(intent);
}
}; private TimerTask timertask = new TimerTask(){
public void run(){
Message message = new Message();
handler.sendMessage(message);
}
}; }

该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. Java中常量小知识

    常量分类:常量分为静态常量,非静态常量(全局常量),局部常量 静态常量:要么定义的时候赋初值,要么在静态代码块中赋值 非静态常量:要么在定义的时候赋初值,要么在代码块中赋值 局部常量:可以在定义时赋初 ...

  2. git 操作简明扼要,命令不需要多,够用就行

    提升能力最快的方法就是做项目. 从前使用svn时,最开始是自己看网上教程,只会一个从服务端checkout文件,update一下,commit一下,后来使用到了分支,感觉好了不少,感觉svn还挺不错的 ...

  3. java 线程的终止与线程中断

    关于线程终止: 1.一般来讲线程在执行完毕后就会进入死亡状态,那该线程自然就终止了. 2.一些服务端的程序,可能在业务上需要,常驻系统.它本身是一个无穷的循环,用于提供服务.那对于这种线程我们该如何结 ...

  4. Servlet Filter

    Filter : Java中的Filter 并不是一个标准的Servlet ,它不能处理用户请求,也不能对客户端生成响应. 主要用于对HttpServletRequest 进行预处理,也可以对Http ...

  5. Command /usr/bin/codesign failed with exit code 1

    刚刚碰到相同的问题,自己解决了,很简单,profile冲突,(自己遇到的现象是之前的profile关联的certificate过期,然后重新生成 了certificate和更新了profile.但是是 ...

  6. java中得到classpath和当前类的绝对路径的一些方法(路径中的%20"进行替换空格)

    原网址:http://blog.csdn.net/shendl/article/details/1427475 (注意:利用下面方式得到路径,如果路径中有空格字符, 那么会有"%20&quo ...

  7. Java多线程系列--“基础篇”05之 线程等待与唤醒

    概要 本章,会对线程等待/唤醒方法进行介绍.涉及到的内容包括:1. wait(), notify(), notifyAll()等方法介绍2. wait()和notify()3. wait(long t ...

  8. ansible入门

    前言 最近看了一下ansible,挺火的一个配置管理工具,对比老大哥puppet,使用起来要简单一些,并且可以批量执行命令,对比同是python语言编写的saltstack,不需要安装客户端(基于pa ...

  9. 理解SQL Server是如何执行查询的 (2/3)

    查询执行的内存授予(Query Execution Memory Grant) 有些操作符需要较多的内存才能完成操作.例如,SORT.HASH.HAS聚合等.执行计划通过操作符需要处理数据量的预估值( ...

  10. Python生成二维码脚本

    简单的记录下二维码生成和解析的Python代码 依赖下面三个包: PIL(图像处理包,安装:pip install PIL) qrcode(二维码生成包,安装:pip install qrcode) ...