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. Redis快速入门详解

    Redis入门详解 Redis简介 Redis安装 Redis配置 Redis数据类型 Redis功能 持久化 主从复制 事务支持 发布订阅 管道 虚拟内存 Redis性能 Redis部署 Redis ...

  2. JavaScript语法、对话框。分支语句

    一.用法 其所在的位置有三块,1.head里面  2.body里面  3.</html>后   一般都写在</html>后 其用法为<script></scr ...

  3. EXCEL的导出

    具体实现代码: protected void EXCEL_Click(object sender, EventArgs e) { ToExcel(GridView1); } public void T ...

  4. iOS----单例模式(Singleton)

    单例的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 1.单例模式的要点: 显然单例模式的要点有三个:一是某个类只能有一个实例:二是 ...

  5. ASP.NET MVC3 模板页的使用

    占位符的使用: 下面是一个模板页 _Layout.cshtml <!DOCTYPE html> <html> <head> @RenderSection(" ...

  6. andriod adt和andriod sdk

    今天搭建appium的环境,没有太明白andriod adt和andriod sdk分别是什么东西,经过与开发沟通,大致了解如下,这里记录一下,免得过几天就搞忘了. andriod adt是一个插件, ...

  7. CCNA基础 IP地址子网划分

    计算机是一个非常神奇的物品,它的核心算法是凌驾于任何代码架构.然而互联网网络( Internat )作为整个生态的基础资源.什么?你还不会子网划分? 没关系,看到子网掩码不要怕.因为它无非就是问你 & ...

  8. Android入门(十八)服务

    原文链接:http://www.orlion.ga/674/ 一.定义一个服务 创建一个项目ServiceDemo,然后在这个项目中新增一个名为 MyService的类,并让它继承自 Service, ...

  9. JSP网站开发基础总结《十》

    经过上一篇的介绍相信大家对JSP提供的过滤器一定有了一个概念,本篇我们就一起再来研究一下关于创建多个过滤器时,如果有两个以上过滤器的过滤规则相同,那么这些过滤器的执行顺序如何呢?答案是根据我们在web ...

  10. Tools - VirtualBox

    为CentOS虚拟机安装增强功能 启动CentOS虚拟机,点击"菜单 -> 设备 -> 安装增强功能". vboxadd的映像文件将会被挂载到虚拟机,在桌面也可以看到, ...