以前对这个东西很感兴趣,因为确实方便,如今有时间了来做一个例子

首先要定义一个layout(widgetview.xml)和一个配置文件(widgetconfig.xml)

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="时间显示" /> </LinearLayout>
 <?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget"
android:minHeight="40dp"
android:minWidth="100dp"
android:updatePeriodMillis="864000" > </appwidget-provider>

然后再实现一个widgetProvider

 package com.example.widgettest;

 import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent; public class WidgetProvider extends AppWidgetProvider {
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
// widget被移除
} @Override
public void onDisabled(Context context) {
super.onDisabled(context);
// 最后一个被移除
context.stopService(new Intent(context, TimerService.class));
} @Override
public void onEnabled(Context context) {
super.onEnabled(context);
// widget添加到屏幕上
context.startService(new Intent(context, TimerService.class));
} @Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
} @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
// 刷新widget
}
}

通过一个service对provider进行刷新

 package com.example.widgettest;

 import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask; import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews; public class TimerService extends Service {
private Timer timer;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
} @Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate(); timer = new Timer();
timer.schedule(new TimerTask() { @Override
public void run() {
updateViews();
}
}, 0, 1000);
} private void updateViews() {
String time = sdf.format(new Date());
RemoteViews rv = new RemoteViews(getPackageName(), R.layout.widget);
rv.setTextViewText(R.id.tv, time);
AppWidgetManager manager = AppWidgetManager
.getInstance(getApplicationContext());
ComponentName cn = new ComponentName(getApplicationContext(),
WidgetProvider.class);
manager.updateAppWidget(cn, rv);
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
timer = null;
} }

最后要注册AndroidManifest文件

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.widgettest"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="23" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <service android:name="com.example.widgettest.TimerService" >
</service> <receiver android:name="com.example.widgettest.WidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter> <meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widgetconfig" />
</receiver>
</application> </manifest>

编写android的widget的更多相关文章

  1. Android 桌面Widget开发要点(时间日期Widget)

    最近需要编写一个日期时间的桌面Widget用来关联日历程序,以前很少写桌面Widget.对这方面技术不是很熟悉,今天花时间重新整理了一下,顺便把编写一个简单时间日期程序过程记录下来. 桌面Widget ...

  2. Android App Widget的简单使用

    App Widget是一些桌面的小插件,比如说天气和某些音乐播放应用,放到桌面去的那部分: 例如: 实现步骤及代码如下: (1)首先,在AndroidManifest.xml中声明一个App Widg ...

  3. Visual Studio 开始支持编写 Android 程序并自带 Android 模拟器【转载】

    原文地址 本文内容 为什么需要一个 Android 模拟器 针对 Visual Studio Android 模拟器的调试 Visual Studio Android 模拟器的传感器模拟和其他功能 A ...

  4. 用Eclipse编写Android程序的代码提示功能

    用Eclipse编写Android程序的代码提示功能主要是在java和xml文件中,有时候会失效,默认的提示功能有限. 1)java文件自动提示     Window->Preferences- ...

  5. 20162311 编写Android程序测试查找排序算法

    20162311 编写Android程序测试查找排序算法 一.设置图形界面 因为是测试查找和排序算法,所以先要有一个目标数组.为了得到一个目标数组,我设置一个EditText和一个Button来添加数 ...

  6. [转]编写 android.mk 中 LOCAL_C_INCLUDES 的技巧

    看原文请移步:编写 android.mk 中 LOCAL_C_INCLUDES 的技巧 在编写android.mk的过程中,免不了要修改LOCAL_C_INCLUDES来设置头文件的include目录 ...

  7. [转]编写Android.mk中的LOCAL_SRC_FILES的终极技巧

    希望看原文的请移步:[原创]编写Android.mk中的LOCAL_SRC_FILES的终极技巧 问题的引入 在使用NDK编译C/C++项目的过程中,免不了要编写Android.mk文件,其中最重要的 ...

  8. 利用Android Studio编写 Android上的c与c++程序

    利用Android Studio编写 Android上的c与c++程序 (2017-05-22 19:01:20) 转载▼ 标签: android 分类: Android开发 原文链接: http:/ ...

  9. 自己编写Android Studio插件 别停留在用的程度了(转载)

    转自:自己编写Android Studio插件 别停留在用的程度了 1概述 相信大家在使用Android Studio的时候,或多或少的会使用一些插件,适当的配合插件可以帮助我们提升一定的开发效率,更 ...

随机推荐

  1. 用C++实现绘制标尺的方法,使用了递归

    在这个例子当中将使用递归来实现一个打印标尺刻度的方法.首先是递归,函数调用其本身就叫递归,在需要将一项工作不断分为两项较小的.类似的工作时,递归非常有用,递归的方法被称为分而治之策略. 下面是一个wi ...

  2. iOS便捷开发工具分享

    项目/代码优化工具 1.objec_dep,可以了解项目中各个类的关联信息,了解项目中无效文件,知道双向应用的文件. 下载地址: https://github.com/nst/objc_dep 2.b ...

  3. VLD(Visual LeakDetector)内存泄露库的使用

    VLD简介 由于C/C++语言没有所谓的垃圾收集器,内存的分配和释放都需要程序员自己来控制,这会给C/C++程序员带来一定的困难.当您的程序越来越复杂时,它的内存管理也会变得越来越困难.内存泄漏.内存 ...

  4. Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())

    在Qt中设置widget背景颜色或者图片方法很多种:重写paintEvent() , 调色板QPalette , 样式表setStyleSheet等等. 但是各种方法都有其注意事项,如果不注意则很容易 ...

  5. [LeetCode#274]H-Index

    Problem: Given an array of citations (each citation is a non-negative integer) of a researcher, writ ...

  6. 【转】Android Building System 总结 - 一醉千年 - CSDN博客

    原文网址:http://www.360doc.com/content/15/0314/23/1709014_455175716.shtml  Android Building System 总结 收藏 ...

  7. 命令行利器Tmux

    Tmux是一个优秀的终端复用软件,类似GNU Screen,但是对两个软件评价已经是一边倒了,大多数人认为tmux功能更加强大,使用更加方便. Tmux不仅可以提高终端工作效率,是服务器管理工作必不可 ...

  8. HDU-2176 取(m堆)石子游戏

    http://acm.hdu.edu.cn/showproblem.php?pid=2176 第三种博弈,但一定要注意优化时间 取(m堆)石子游戏 Time Limit: 3000/1000 MS ( ...

  9. Sicily1059-Exocenter of a Trian

    代码地址: https://github.com/laiy/Datastructure-Algorithm/blob/master/sicily/1059.c 1059. Exocenter of a ...

  10. ReactiveCocoa Tutorial

    ReactiveCocoa Tutorial – The Definitive Introduction: Part 1/2 ReactiveCocoa教程——明确的介绍:第一部分(共两部分) As ...