编写android的widget
以前对这个东西很感兴趣,因为确实方便,如今有时间了来做一个例子
首先要定义一个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的更多相关文章
- Android 桌面Widget开发要点(时间日期Widget)
最近需要编写一个日期时间的桌面Widget用来关联日历程序,以前很少写桌面Widget.对这方面技术不是很熟悉,今天花时间重新整理了一下,顺便把编写一个简单时间日期程序过程记录下来. 桌面Widget ...
- Android App Widget的简单使用
App Widget是一些桌面的小插件,比如说天气和某些音乐播放应用,放到桌面去的那部分: 例如: 实现步骤及代码如下: (1)首先,在AndroidManifest.xml中声明一个App Widg ...
- Visual Studio 开始支持编写 Android 程序并自带 Android 模拟器【转载】
原文地址 本文内容 为什么需要一个 Android 模拟器 针对 Visual Studio Android 模拟器的调试 Visual Studio Android 模拟器的传感器模拟和其他功能 A ...
- 用Eclipse编写Android程序的代码提示功能
用Eclipse编写Android程序的代码提示功能主要是在java和xml文件中,有时候会失效,默认的提示功能有限. 1)java文件自动提示 Window->Preferences- ...
- 20162311 编写Android程序测试查找排序算法
20162311 编写Android程序测试查找排序算法 一.设置图形界面 因为是测试查找和排序算法,所以先要有一个目标数组.为了得到一个目标数组,我设置一个EditText和一个Button来添加数 ...
- [转]编写 android.mk 中 LOCAL_C_INCLUDES 的技巧
看原文请移步:编写 android.mk 中 LOCAL_C_INCLUDES 的技巧 在编写android.mk的过程中,免不了要修改LOCAL_C_INCLUDES来设置头文件的include目录 ...
- [转]编写Android.mk中的LOCAL_SRC_FILES的终极技巧
希望看原文的请移步:[原创]编写Android.mk中的LOCAL_SRC_FILES的终极技巧 问题的引入 在使用NDK编译C/C++项目的过程中,免不了要编写Android.mk文件,其中最重要的 ...
- 利用Android Studio编写 Android上的c与c++程序
利用Android Studio编写 Android上的c与c++程序 (2017-05-22 19:01:20) 转载▼ 标签: android 分类: Android开发 原文链接: http:/ ...
- 自己编写Android Studio插件 别停留在用的程度了(转载)
转自:自己编写Android Studio插件 别停留在用的程度了 1概述 相信大家在使用Android Studio的时候,或多或少的会使用一些插件,适当的配合插件可以帮助我们提升一定的开发效率,更 ...
随机推荐
- 2016030205 - ubuntu安装mysql
ubuntu上安装mysql 1.检查ubuntu上是否已经安装mysql sudo netstat -tap | grep mysql 本机上没有安装mysql 2.安装mysql服务器端和客户端 ...
- Java Fluent Restful API自动化测试框架
这是一个Restful API自动化测试框架,这是一个能让你写出高可读性测试代码的测试框架! 项目目标 话说目前行业内,Restful API自动化测试框架已经不是稀罕物了,各个语言都有自己的实现机制 ...
- Coursera《machine learning》--(14)数据降维
本笔记为Coursera在线课程<Machine Learning>中的数据降维章节的笔记. 十四.降维 (Dimensionality Reduction) 14.1 动机一:数据压缩 ...
- Contest 20140708 testA && testC
testA 输入文件: testA.in 输出文件testA.out 时限2000ms 问题描述: 如果一个数化为一个二进制数之后(没有前导0),0的个数>=1的个数.那么这个数就是方数. E ...
- Match & Catch
Codeforces Round #244 (Div. 2) D:http://codeforces.com/contest/427/problem/D 题意:给你两个串,让你找一个最小的串,并且这个 ...
- 嵌入式C语言头文件的建立与使用
如何正确编写 C 语言头文件和与之相关联的 c 源程序文件,这首先就要了解它们的各自功能. 要理解 C 文件与头文件(即.h)有什么不同之处,首先需要弄明白编译器的工作过程. 一般说来编译器会做以下几 ...
- JSch - Java实现的SFTP(文件下载详解篇)(转)
上一篇讲述了使用JSch实现文件上传的功能,这一篇主要讲述一下JSch实现文件下载的功能.并介绍一些SFTP的辅助方法,如cd,ls等. 同样,JSch的文件下载也支持三种传输模式:OVERWRI ...
- OpenWrt挂载USB储存设备实现Samba共享
没有USB接口的路由器不是好路由器,有了USB接口OpenWrt才有更多的玩法,比如挂载U盘.移动硬盘等USB储存设备实现Samba共享,打造小型家庭服务器. 1.安装与USB相关的软件包: opkg ...
- 关于Unity的ViewSpace(CameraSpace)的坐标系
从昨天开始遇到一个看似很小,但令我苦恼的问题,由于对Unity的坐标系没有直接搞清楚,所以导致一个shader没看懂,于是发了个贴:http://game.ceeger.com/forum/read. ...
- JavaScript高级程序设计9.pdf
Number是数字值对应的引用类型 var numberObject=new Number(10); Number也重写了valueof().toLocaleString().和toString()方 ...