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)的更多相关文章

  1. Android学习笔记(20):时钟(AnalogClock和TextClock)和计时器(Chronometer)

    时钟文本TextClock继承自TextView.是用于显示当前时间的文本框. TextClock支持的XML属性和相关方法 XML属性 相关方法 说明 android:format12Hour se ...

  2. Android 学习笔记之Volley(七)实现Json数据加载和解析...

    学习内容: 1.使用Volley实现异步加载Json数据...   Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...

  3. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  4. android学习笔记36——使用原始XML文件

    XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...

  5. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  6. udacity android 学习笔记: lesson 4 part b

    udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...

  7. Android学习笔记36:使用SQLite方式存储数据

    在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...

  8. Android学习笔记之Activity详解

    1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...

  9. Pro Android学习笔记 ActionBar(1):Home图标区

     Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...

随机推荐

  1. spring-data-elasticsearch使用笔记

    使用spring-data遇到了一些问题,记录一下. spring-data-elasticsearch版本选择 这里有一份官方github上的spring-data-elasticsearch与el ...

  2. TOJ 3031 Multiple

    Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinc ...

  3. 【LDAP】ldap目录服务的命名模型

    ldap的命名模型 命名模型规定了在目录中如何组织和表示条目 1.   目录信息树(DIT) 目录信息树有点类似于DNS的结构.每一个条目都有自己的父条目(因为主条目的父条目是top,所以这句话是成立 ...

  4. 【VMware&Vritualbox】虚拟机安装windows server2016

    一.下载镜像 参考链接:https://blog.csdn.net/yenange/article/details/52981769 http://blog.sina.com.cn/s/blog_10 ...

  5. [转]JS组件系列——BootstrapTable 行内编辑解决方案:x-editable

    本文转自:http://www.cnblogs.com/landeanfen/p/5821192.html 阅读目录 一.x-editable组件介绍 二.bootstrapTable行内编辑初始方案 ...

  6. Maven入门之简介与安装

    一.Maven简介 1.什么是Maven? Maven是一个项目管理工具和集成编译工具,它主要包含如下内容: –一个项目对象模型(Project Object Model), –一组标准集合, –一个 ...

  7. VS2012 无法启动 IIS Express Web

    用记事本打开项目的.csproj文件,定位到<WebProjectProperties>,把关于IIS的配置<DevelopmentServerPort>.<Develo ...

  8. 【Android】6.0 添加Menu菜单组件、Intent启动活动、显式Intent、隐式Intent

    1.0 在helloworld项目基础上创建活动SecondActivity: 2.0 其中main.xml: <?xml version="1.0" encoding=&q ...

  9. 创建简单的node服务器

    昨天咱们说了封装ajax,今天咱们说一下 自己创建一个建议的node服务器: 话不多说直接上代码: var http = require('http') //对URL 解析为对象//1.导入模块 UR ...

  10. Arcgis flex 切片地图麻点

    在arcgis server中发布地图切片完成后,有时候在访问地图的时候会出现很多麻点, 其实是你切片的时候没有注意到一些选项.... 默认的切片是PNG8,说到这可能就明白了吧,png8的色彩范围: ...