APP Widget的开发
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的开发的更多相关文章
- [安卓开发]App Widget开发入门指导
本节所要讲的主要内容包括Android桌面小部件.App Widget的开发入门指导,并通过一个简单实例的形式来直观的讲解App Widget. 一.Widget .App Widget .Web A ...
- Android开发之创建App Widget和更新Widget内容
App WidgetsApp Widgets are miniature application views that can be embedded in other applications (s ...
- App Widget简单应用
首先后台进程创建一个PendingIntent对象,其中PendingIntent中包含一个真正的Intent,创建完成后将此PendingIntent对象交给桌面控件所在的进程,当用户点击桌面控件或 ...
- App Widget
AppWidgetProviderInfo对象: 为App Widget提供元数据(描述数据的数据,如XML.关系型数据的表结构),包括布 局,更新频率等数据.这个对象被定义在XML文件当中: App ...
- 一个App Widget实例第一次创建时被调用
事实上已经有很多的所谓的路由框架做到这一点,我也没有去研究别的,加上一直对backbone这个框架的评价不错,所以就琢磨着怎么用它实现我所需要的SPA的url管理了. 比如,你可能会说"如果 ...
- 实战使用Axure设计App,使用WebStorm开发(6) – 迈向后端
系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求 实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目 实战使 ...
- 实战使用Axure设计App,使用WebStorm开发(5) – 实现页面功能
系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求 实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目 实战使 ...
- 实战使用Axure设计App,使用WebStorm开发(4) – 实现页面UI
系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求 实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目 实战使 ...
- 实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目
系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求 实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目 实战使 ...
随机推荐
- Atitit 作用域的理解attilax总结
Atitit 作用域的理解attilax总结 1.1. 作用域是指对某一变量和方法具有访问权限的代码空间, 1 1.2. 作用域的使用提高了程序逻辑的局部性,增强程序的可靠性,减少名字冲突.1 1.3 ...
- instanceof, typeof, & Object.prototype.toString
/** * * @authors Your Name (you@example.org) * @date 2016-11-18 09:31:23 * @version $Id$ */instanceo ...
- jeasyUI的treegrid批量删除多行(转载)
看上去,JavaScript的变量类型,也可以分为值类型和引用类型.赋值操作中,值类型,各自独立,互不干涉:引用类型,指针而已,大家指向同一个对象. 为什么这样说呢? 我是从jeasyUI的treeg ...
- POJ3069 POJ2586 解题报告(异曲同工的贪心算法)
[POJ 3069](2586见下) 原题在此:http://poj.org/problem?id=3069 题目大意: 一个直线上有N个点.点i的距离是Xi.从这些点中选取若干个加上标记.要求:对于 ...
- python导入模块和包的使用
做项目的时候经常会要求做到模块化管理,即同样功能的代码放到同一个文件夹下,以便于方便管理,相信很多人对模块的引用都模糊不清,今天鄙人在这里就总结下. 一.模块导入的使用 在同一个文件夹下有两个文件分别 ...
- wamp2.5 配置Apache允许外网访问
找到<Directory "e:/wamp/www/">节点,在里面添加Require all granted
- javscript对cookie的操作,以及封装
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- php基础教程-数据类型
PHP 支持八种原始类型(type). 四种标量类型: string(字符串) integer(整型) float(浮点型,也作 double ) boolean(布尔型) 两种复合类型: array ...
- 转载-centos网络配置(手动设置,自动获取)的2种方法
转载地址:http://blog.51yip.com/linux/1120.html 重新启动网络配置 # service network restart 或 # /etc/init.d/networ ...
- 材价看板(1)- 如何建立你的第一个kanban,看看这些暴露的问题你们有没有?
今年负责一个老产品新团队,和几年前的指标组一样,现在的团队没有采用什么研发方法,于是我开始了团队的看板之旅. 12月22日给材价整个部门的产品研发相关人员做了一次kanban工作坊培训, 以及敏 ...