欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android实现widget时钟示例分享

程序员文章站 2022-06-10 19:09:20
一、在 androidmanifest.xml文件中配置widgets: 复制代码 代码如下:

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