android 时钟控件风格,android实现widget时钟示例分享

一、在 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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值