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 ...
随机推荐
- [Scala] Currying
Currying是一種函數式編程技巧, 指的是把接受多個參數的函數變換成接受一個單一參數的函數. 以一個簡單的例子在Scala中實現.. def f(a:Int, b:Int)={ a+b } //f ...
- 查询指定tomcat应用的进程数
假设应用名称为pear,查询指定tomcat应用pear的进程数: ps -ef |grep "/datong/tomcat-pear/" |grep -v tail | grep ...
- nyoj 1239——引水工程——————【最小生成树 prim】
引水工程 时间限制:2000 ms | 内存限制:65535 KB 难度:3 描述 南水北调工程是优化水资源配置.促进区域协调发展的基础性工程,是新中国成立以来投资额最大.涉及面最广的战略性工 ...
- 深入理解JavaScript系列(2):揭秘命名函数表达式
前言 网上还没用发现有人对命名函数表达式进去重复深入的讨论,正因为如此,网上出现了各种各样的误解,本文将从原理和实践两个方面来探讨JavaScript关于命名函数表达式的优缺点. 简单的说,命名函数表 ...
- 搭建Vue2.0开发环境
1.必须要安装nodejs 2.搭建vue的开发环境 ,安装vue的脚手架工具 官方命令行工具 npm install --global vue-cli / cnpm install --global ...
- intent 活动之间穿梭
1.从当前activity,跳转到当前应用程序的activity Intent intent = new Intent(MainActivity.this, Intent2Activity.class ...
- mysql主从复制报错 :Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
在配置mysql主从复制时,想通过 grant replication slave on bbs.* to 'bbs'@'192.168.1.3' identified by '123456'; 来限 ...
- Supper关键字
java中的super关键字是一个引用变量,用于引用直接父类对象. 每当创建子类的实例时,父类的实例被隐式创建,由super关键字引用变量引用. java super关键字的用法如下: super可以 ...
- 移动端点击a链接出现蓝色背景问题解决
a:link, a:active, a:visited, a:hover { background: none; -webkit-tap-highlight-color: rgba(0,0,0,0); ...
- CKRule业务规则管理系统部署说明
1. 程序包说明 软件是使用WinForm开发的,包含服务端和客户端,服务端部署在IIS上面,客户端可以在已经安装.Net4.0的windows上面运行. 1.1. 服务端程序包 CKBrmsS ...