Android学习笔记_33_Widget时钟(MetaData)
Widgets在文档docs\guide\topics\appwidgets\index.html下
Android中AppWidget的分析与应用:AppWidgetProvider
一、在 AndroidManifest.xml文件中配置
Widgets:
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.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=".TimeWidgetProvider" >
- <intent-filter>
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
- </intent-filter>
- <meta-data
- android:name="android.appwidget.provider"
- android:resource="@xml/timewidget_info" />
- </receiver>
- <service android:name=".TimerService"></service>
- </application>
- </manifest>
二、在项目的res目录下建立xml目录,并且创建 timewidget_info.xml 文件,内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
- android:initialLayout="@layout/time_appwidget"
- android:minHeight="40dp"
- android:minWidth="40dp"
- android:updatePeriodMillis="0" />
三、在layout文件夹下建立文件time_appwidget.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"
- android:background="@drawable/rectangle"
- >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/textView"
- android:text="current time"
- />
- </LinearLayout>
四、在drawable文件夹下建立rectangle.xml文件(这部可以省略,主要是为了美化TextView控件,渐变效果):
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle" >
- <!-- 设置弧度 -->
- <corners android:radius="9dp" />
- <gradient
- android:angle="270"
- android:endColor="#5EADF4"
- android:startColor="#B3F0FF" />
- <padding
- android:bottom="5dp"
- android:left="5dp"
- android:right="5dp"
- android:top="5dp" />
- <stroke
- android:dashGap="1dp"
- android:dashWidth="10dp"
- android:width="6dp"
- android:color="#0000FF" />
- </shape>
五、后台代码实现:
- package com.example.widget;
- import android.appwidget.AppWidgetManager;
- import android.appwidget.AppWidgetProvider;
- import android.content.Context;
- import android.content.Intent;
- public class TimeWidgetProvider extends AppWidgetProvider {
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int[] appWidgetIds) {
- super.onUpdate(context, appWidgetManager, appWidgetIds);
- }
- //当一个Widgets时会被调用
- public void onDeleted(Context context, int[] appWidgetIds) {
- // TODO Auto-generated method stub
- super.onDeleted(context, appWidgetIds);
- }
- //第一次往桌面添加Widgets时会被调用,之后添加同类型Widgets不会被调用
- public void onEnabled(Context context) {
- context.startService(new Intent(context, TimerService.class));
- }
- //从桌面上删除最后一个Widgets时会被调用
- public void onDisabled(Context context) {
- context.stopService(new Intent(context, TimerService.class));
- }
- }
- package com.example.widget;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.annotation.SuppressLint;
- 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;
- @Override
- public void onCreate() {
- super.onCreate();
- timer = new Timer();
- timer.schedule(new MyTimerTask(), 0, 1000);
- }
- private final class MyTimerTask extends TimerTask{
- @SuppressLint("SimpleDateFormat")
- @Override
- public void run() {
- SimpleDateFormat sdf= new SimpleDateFormat("hh:mm:ss");
- String time = sdf.format(new Date());
- //获取Widgets管理器
- AppWidgetManager widgetManager =AppWidgetManager.getInstance(getApplicationContext());
- //widgetManager所操作的Widget对应的远程视图即当前Widget的layout文件
- RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.time_appwidget);
- remoteView.setTextViewText(R.id.textView, time);
- //当点击Widgets时触发的世界
- //remoteView.setOnClickPendingIntent(viewId, pendingIntent)
- ComponentName componentName = new ComponentName(getApplicationContext(),TimeWidgetProvider.class);
- widgetManager.updateAppWidget(componentName, remoteView);
- }
- }
- @Override
- public void onDestroy() {
- timer.cancel();
- timer=null;
- super.onDestroy();
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- }
Android学习笔记_33_Widget时钟(MetaData)的更多相关文章
- Android学习笔记(20):时钟(AnalogClock和TextClock)和计时器(Chronometer)
时钟文本TextClock继承自TextView.是用于显示当前时间的文本框. TextClock支持的XML属性和相关方法 XML属性 相关方法 说明 android:format12Hour se ...
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- Android学习笔记进阶之在图片上涂鸦(能清屏)
Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- udacity android 学习笔记: lesson 4 part b
udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...
- Android学习笔记36:使用SQLite方式存储数据
在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...
- Android学习笔记之Activity详解
1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...
- Pro Android学习笔记 ActionBar(1):Home图标区
Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...
随机推荐
- python实现excel转json的例子
python实现excel转json的例子(改进版) 由于数值策划给出数值是excel表格,但前台flash程序用的又是json格式.服务器也用了json格式,而json又是utf-8编码的,用C++ ...
- 《springcloud 四》服务保护机制
服务保护机制SpringCloud Hystrix 微服务高可用技术 大型复杂的分布式系统中,高可用相关的技术架构非常重要. 高可用架构非常重要的一个环节,就是如何将分布式系统中的各个服务打造成高可用 ...
- 在 Flask 应用中使用 gevent
在 Flask 应用中使用 gevent 普通的 flask 应用 通常在用 python 开发 Flask web 应用时,使用 Flask 自带的调试模式能够给开发带来极大便利.Flask 自带的 ...
- 深入理解JavaScript系列(27):设计模式之建造者模式
介绍 在软件系统中,有时候面临着“一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成:由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法确相对稳定. ...
- WPF - MVVM 之TreeView
在项目中使用OnPropertyChanged方法,最简单的实例: private event PropertyChangedEventHandler PropertyChanged; protect ...
- 获取当前的日期时间的js函数,格式为“yyyy-MM-dd hh:mm:ss”
//获取当前的日期时间函数,格式为“yyyy-MM-dd hh:mm:ss” function getNowFormatDate(date) { if (date == null) { var dat ...
- Android使用主题属性引发的问题
最近在做一个项目的Porting.直接改变了应用的Theme,最没有仔细的检查.结果应用在某些场景下直接就Crash了.还好,通过Log可以看到是在Inflate某个资源的时候出错导致的.通过定位资源 ...
- 个人MySQL股票数据库的建立日记
#!/usr/bin/python# -*- coding: UTF-8 -*- import tushare as tsfrom sqlalchemy import create_engine co ...
- JS图片赖加载例子
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- matlab练习程序(单源最短路径Dijkstra)
图的相关算法也算是自己的一个软肋了,当年没选修图论也是一大遗憾. 图像处理中,也有使用图论算法作为基础的相关算法,比如图割,这个算法就需要求最大流.最小割.所以熟悉一下图论算法对于图像处理还是很有帮助 ...