iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >android实现widget时钟示例分享
  • 400
分享到

android实现widget时钟示例分享

示例Android 2022-06-06 10:06:44 400人浏览 泡泡鱼
摘要

一、在 AndroidManifest.xml文件中配置Widgets: 代码如下:<manifest xmlns:android="Http://schemas.an

一、在 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获取设备CPU核数、时钟频率以及内存大小的方法Android多功能时钟开发案例(实战篇)Android 仿日历翻页、仿htc时钟翻页、数字翻页切换效果android高仿小米时钟(使用Camera和Matrix实现3D效果)Android多功能时钟开发案例(基础篇)Android实现简单时钟View的方法Android仿小米时钟效果Android画个时钟玩玩Android编程基于自定义控件实现时钟功能的方法Android canvas自定义实现时钟效果


--结束END--

本文标题: android实现widget时钟示例分享

本文链接: https://www.lsjlt.com/news/27173.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作