一、在 AndroidManifest.xml文件中配置Widgets:

package="com.example.widget"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="8"

android:targetSdkVersion="17" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name="android.appwidget.provider"

android:resource="@xml/timewidget_info" />

二、在项目的res目录下建立xml目录,并且创建 timewidget_info.xml 文件,内容如下:

android:initialLayout="@layout/time_appwidget"

android:minHeight="40dp"

android:minWidth="40dp"

android:updatePeriodMillis="0" />

三、在layout文件夹下建立文件time_appwidget.xml:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:background="@drawable/rectangle"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/textView"

android:text="current time"

/>

四、在drawable文件夹下建立rectangle.xml文件(这部可以省略,主要是为了美化TextView控件,渐变效果):

android:shape="rectangle" >

android:angle="270"

android:endColor="#5EADF4"

android:startColor="#B3F0FF" />

android:bottom="5dp"

android:left="5dp"

android:right="5dp"

android:top="5dp" />

android:dashGap="1dp"

android:dashWidth="10dp"

android:width="6dp"

android:color="#0000FF" />

五、后台代码实现:

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(),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;

}

}

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

小编个人微信号 jb51ccc

喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!

android 时钟控件风格,android实现widget时钟示例分享相关推荐

  1. Android Studio利用时钟控件AnalogClock显示模拟时钟以及TextClock显示数字时钟

    前言 利用时钟控件AnalogClock快速制作一个模拟时钟.利用TextClock显示数字时钟. 一.AnalogClock是什么? AnalogClock继承的是View,可重写OnDraw方法. ...

  2. Android 自定义时钟控件 时针、分针、秒针的绘制这一篇就够了

    前言 对于 Android 开发者来说,自定义 View 是绕不开的一个坎.二对一自定义 View 自定义时钟必然是首选,那么我们该如何绘制自定义时钟呢?本篇我结合 github 上一个有趣的三方库, ...

  3. [发布] 多选控件和时钟控件

    关键字:自定义控件(Custom Control),C++,WIN32 SDK 本文发布的是我在工作中开发的自定义控件.第一个是多选控件,该控件主要启发来自于 ExplorerBar,即资源管理器左侧 ...

  4. C#时钟控件 (C# Clock Control)

    周末实现了一个比较漂亮的时钟控件,如下图: 功能: (1)自定义样式(时钟边框颜色,小时.分钟.秒针的颜色自定义,自定义时钟内部填充色,刻度及刻度值颜色,刻度可使用矩形或圆形). (2)可作为图片水印 ...

  5. C 时钟控件 (C Clock Control)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 周末实现 ...

  6. c# winform LED数字时钟控件

    包含颜色选择器,日历控件(类似于Google日历),数字时钟,LED等控件 1.日历控件       calendar.rar 2.Office 2003中的颜色选择器    OfficePicker ...

  7. Android 自定义模拟时钟控件

    自定义view-透明模拟时钟显示 项目中要用到模拟时钟的显示,查了一些资料根据自己的需要进行了自定义view 思路:重写view,1.根据控件的宽高进行获取模拟时钟的半径大小.2.重写onDraw方法 ...

  8. Android自定义时钟控件

    项目要求访问网络是等待状态要做时钟的样子,经过不懈努力,终于写出来了,现分享出来,功能比较全,直接拷贝代码就可以用,仅供有这种需求的码农们参考,如果采纳,请点个赞,谢谢支持. 效果图 主Activit ...

  9. android按钮控件常见问题,Android的基本控件和Activity的应用总结

    Android的基本控件 常用界面控件 TextView 显示文本信息 button 普通按钮 EditText 可编辑的文本框组件(输入框) ImageView 用于显示图片 ImageBUtton ...

最新文章

  1. 职业中专的计算机综合应用,职业中专计算机教学的思考
  2. java选中一格_java-选中排序(新手)
  3. 合并报表编制采用的理论_合并财务报表的简易编制方法,真是太好用了!财务,会计一起看...
  4. C++设计模式-备忘录模式
  5. Spring classPath:用法
  6. Speedment 简介
  7. win7下Visual Studio 2012 update4、AutoCAD 2016和二次开发插件ObjectARX2016环境安装及配置
  8. 十大经典算法图解(详细版)
  9. 【2018华科机试】十二进制
  10. 辽宁大连最好的计算机大学排名,2020年大连市最好大学排行榜:20所高校上榜,东北财经大学居第二...
  11. 0314-html img em i stong b标签应用
  12. 计算机软件录音注意事项,GOLDWAVE录音软件使用教程
  13. 红米电竞路由器AX5400与红米路由器AX5400哪个值得买? 两者区别介绍
  14. 快应用如何接入微信支付
  15. HDU2102(KB2-I)
  16. 佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢?
  17. python爬虫入门—selenuim自动登录qq邮箱
  18. AI神经网络-CNN基本结构分析( Conv层、Pooling层、FCN层/softmax层)
  19. Bootstrap方法在R语言中的运用
  20. 简单介绍如何应用【Poi-tl】将【个人简历】导出为【docx】格式的【word】文档

热门文章

  1. 2021,noip考点总结,加油
  2. flv在线录制 视频转换网站开发文档
  3. 谁与争锋——2020款沃尔沃与2019款别克君越
  4. 加密解密:公钥私钥过程详解
  5. 手写webpack插件,小白入门 —— md文档转成html
  6. 【OpenCV】中copyto()函数的使用方法理解
  7. 阿里云redis数据恢复
  8. Android面试---OkHttp原理
  9. 大课深度复盘、解密研发效率之道 | 第42届MPD工作坊成都站日程公布!
  10. 什么是公网IP?公网IP和内网IP的区别