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 项目 实战使 ...
随机推荐
- sizeof && strlen 的区别
本文主要记录了 sizeof 操作符 和 strlen() 函数的区别,以及各自的用途.(在下才疏学浅,发现错误,还请留言指正) sizeof 和 strlen 的区别 示例代码如下: #includ ...
- CAR
24.编写一个Car类,具有String类型的属性品牌,具有功能drive: 定义其子类Aodi和Benchi,具有属性:价格.型号:具有功能:变速: 定义主类E,在其main方法中分别创建Aodi和 ...
- vs如何在C++中调用Lua
最近Cocos2dx的学习卡壳了,一般的照抄代码我不想写上来,但想示例也想得我头晕...为了放松大脑调整状态于是开始学习Lua.Lua的语法学习还是比较简单的,学过javascript或者vbscri ...
- Atiti attilax主要成果与解决方案与案例rsm版 v2
Atiti attilax主要成果与解决方案与案例rsm版 v2 1. ##----------主要成果与解决方案与 参与项目1 ###开发流程与培训系列1 #-----组织运营与文化建设系列1 # ...
- 大数据时代的IT架构设计
大数据时代的IT架构设计(来自互联网.银行等领域的一线架构师先进经验分享) IT架构设计研究组 编著 ISBN 978-7-121-22605-2 2014年4月出版 定价:49.00元 208页 ...
- 使用动态类型dynamic让你的省了很多临时类
客户端与服务端的数据交互使用的数据格式是json格式,为了使客户端与服务端有类对应关系,进行序列化,所以总要定义一些类,使用动态类型dynamic可以不必要定义那么多类. 测试代码: using Sy ...
- 找到SQL Server数据库历史增长信息
很多时候,在我们规划SQL Server数据库的空间,或向存储方面要空间时,都需要估算所需申请数据库空间的大小,估计未来最简单的办法就是看过去的趋势,这通常也是最合理的方式. 通常来讲 ...
- 如何用sublime 编写sass
使用了Sublime Text也有一段时日了,然后在现在而言,小觉的coding工具已经非其莫属了,接着小觉因为近期忙着项目的原因,同时还要抽空编辑博客的原因,就暂时把它放在一旁了,现在偶然想起也就说 ...
- Shader LOD
设置:单个设置Shader.maximumLOD.全局设置Shader.globalMaximumLOD.QualitySettings里面的Maximum LODLevel 原理:小于指定值的sha ...
- Android悬浮窗口的实现
效果图:(悬浮框可拖动) 在项目开发中有一个需求:弹出悬浮窗后,响应悬浮窗的事件再弹出对话框,但是对话框怎么也不显示.也就是说在弹出悬浮框的同时,不能再弹出对话框,可能的原因: 1.悬浮框的焦点在最前 ...