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;
    }
}

标签:android,widget,时钟
0
投稿

猜你喜欢

  • javaweb实现在线支付功能

    2022-02-08 19:17:34
  • 一键清除maven仓库中下载失败的jar包的实现方法

    2023-08-13 03:43:59
  • Android如何防止apk程序被反编译(尊重劳动成果)

    2022-02-08 02:18:04
  • Android实现指针刻度转盘

    2022-06-08 07:41:42
  • Java 实战项目之精品养老院管理系统的实现流程

    2022-05-30 08:18:11
  • Springmvc模式上传和下载与enctype对比

    2022-11-08 09:14:17
  • Java简单实现UDP和TCP的示例

    2021-08-02 14:57:48
  • Java中StringBuilder与StringBuffer使用及源码解读

    2022-05-16 09:11:18
  • 使用javaMail实现发送邮件

    2023-01-20 21:22:26
  • C#中重载相等(==)运算符示例

    2023-01-11 15:10:36
  • Android编程之控件状态配置文件实例

    2023-07-26 02:38:54
  • Android CountDownTimer实现定时器和倒计时效果

    2023-12-09 02:11:28
  • JVM类加载,垃圾回收

    2022-08-16 02:44:36
  • spring boot 日志配置详解

    2023-09-24 00:23:11
  • 微信公众平台(测试接口)准备工作

    2023-05-24 15:51:06
  • 简单介绍java中equals以及==的用法

    2023-01-28 07:47:41
  • Java源码解析之详解ImmutableMap

    2023-11-23 08:06:07
  • 如何用java程序(JSch)运行远程linux主机上的shell脚本

    2023-11-24 12:35:58
  • JavaWeb项目部署到服务器详细步骤详解

    2023-11-29 11:15:20
  • springboot 项目启动后无日志输出直接结束的解决

    2023-05-25 02:28:21
  • asp之家 软件编程 m.aspxhome.com