android实现widget时钟示例分享
发布时间:2021-09-09 09:24:55
一、在 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;
}
}


猜你喜欢
- 本文实例讲述了C#编程之事务用法。分享给大家供大家参考,具体如下:ado.net2.0的SqlTransaction使用方法/////ado
- 做Java编程,难免会遇到多线程的开发,但是JDK8这个CompletableFuture类很多开发者目前还没听说过,但是这个类实在是太好用
- 本文实例讲述了JavaWeb 网上书店 注册和登陆功能。分享给大家供大家参考,具体如下:工具:Eclipse + Navicat源码地址:h
- WebService服务端代码public class WebServiceDemo : System.Web.Services.WebSe
- 在分布式系统中,我们会需要 ID 生成器的组件,这个组件可以实现帮助我们生成顺序的或者带业务含义的 ID。目前有很多经典的 ID 生成方式,
- PS:公司的业务中有个超级大的作业就是把OFFICE文档转成PDF,我猜之前没程序猿们,公司那些人应该是一个个手动转。强烈为猿们感叹,帮你们
- 通过 SpringBoot 实现了表单下的文件上传,前后端分离情况下的文件上传。本案例不连接数据库,只做基本的文件上传操作。在 Spring
- 1、什么是FeignFeign 是 Spring Cloud Netflix 组件中的一个轻量级 RESTful 的 HTTP 服务客户端,
- vscode Java 开发环境配置博客地址VsCode官网教程系统需安装jdk1.8,配置好环境变量JAVA_HOME 打开vscode,
- 最近做一个需求,需求中的bean只用于生成一次json使用,所以想通过配置来动态的生成,查了一下,java还真有这个实现。java动态的生成
- 首先,查到java里文件重命名的方法为:renameTo();我将180张图片放在d:\\backup下,用下面的程序进行重命名:publi
- 如果想你写的程序随系统开机一起启动的话,那么你可以照下面这个方法来做。 RunWhenStart(false, Applicati
- 本文实例讲述了Android编程中activity启动时出现白屏、黑屏问题的解决方法。分享给大家供大家参考,具体如下:默认情况下 activ
- 一个系统上线,肯定会或多或少的存在异常情况。为了更快更好的排雷,记录请求参数和响应结果是非常必要的。所以,Nginx 和 Tomcat 之类
- 随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染、先后端分离的形态,而且前端技术和后端技术在各自的道路上越走越远
- Strut2判断是否是AJAX调用1. AJAX与传统Form表单实际上,两者一般都是通过HTTP的POST请求。区
- 本文实例为大家分享了java实现时间与字符串之间转换的具体代码,供大家参考,具体内容如下1. long字符串转换成yyyy-MM-dd HH
- 序章简介:bean的加载控制指根据特定情况对bean进行选择性加载以达到适用项目的目标。根据之前对bean加载的八种方式,其中后面四种是可以
- 下面通过代码看下springboot 跨域配置类,代码如下所示:ackage org.fh.config;import java.io.IO
- public static String getCharset(File file) { &n