温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
ShowTimeNowAppWidget
package com.zhy.shortcut;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.RemoteViews;

public class ShowTimeNowAppWidget extends AppWidgetProvider {

    
private static final String TAG = "ShowTimeNowAppWidget";

    
    Handler handler;
    
    AppWidgetManager widgetManager;
    RemoteViews remoteViews;
    
    Context context;
    
    
    
    
    @Override
    
public void onDeleted(Context context, int[] appWidgetIds) {
        
super.onDeleted(context, appWidgetIds);

        Log.d(TAG, 
"---------onDeleted----------");

    }

    @Override
    
public void onDisabled(Context context) {
        
super.onDisabled(context);
        Log.d(TAG, 
"---------onDisabled----------");
    }

    @Override
    
public void onEnabled(Context context) {
        
super.onEnabled(context);
        Log.d(TAG, 
"---------onEnabled----------");
    }

    @Override
    
public void onReceive(Context context, Intent intent) {
        
super.onReceive(context, intent);
        Log.d(TAG, 
"---------onReceive----------");
    }

    @Override
    
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            
int[] appWidgetIds) {
        
        
super.onUpdate(context, appWidgetManager, appWidgetIds);

        
this.context=context;
        
        
final int appWidgetId=appWidgetIds[0];
        
        
//创建一个RemoteViews
        RemoteViews views=new RemoteViews(context.getPackageName(),R.layout.widgetlayout);
        
        widgetManager
=appWidgetManager;
        remoteViews
=views;
        
        handler
=new Handler(){
            @Override
            
public void handleMessage(Message msg) {
                
super.handleMessage(msg);
                
                
switch (msg.what) {
                
case 0:

                    
//ComponentName component=new ComponentName(ShowTimeNowAppWidget.this.context, ShowTimeNowAppWidget.class);
                    
                    
//更新widget上TextView控件的内容
                    remoteViews.setTextViewText(R.id.widget_time, String.valueOf(msg.obj));
                    
//更新widget
                    widgetManager.updateAppWidget(appWidgetId, remoteViews);
                    
                    
break;
                
default:
                    
break;
                }
            }
        };
        
        handler.post(
new Runnable() {
            
            @Override
            
public void run() {
                Message message
=new Message();
                message.what
=0;
                message.obj
=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date());
                handler.sendMessage(message);
                
                handler.postDelayed(
this1000);
            }
        });
        Log.d(TAG, 
"---------onUpdate----------");
        
    }

}
widgetlayout布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding
="30dip"
    android:layout_width
="match_parent"
    android:layout_height
="wrap_content" >

    
<ImageView
        
android:id="@+id/widget_img"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content"
        android:src
="@drawable/ic_dialog_time" />

    
<TextView
        
android:textSize="16dip"
        android:textColor
="@color/green"
        android:singleLine
="true"
        android:ellipsize
="none"
        android:gravity
="center"
        android:paddingLeft
="5dip"
        android:layout_toRightOf
="@id/widget_img"
        android:layout_alignTop
="@id/widget_img"
        android:layout_alignBottom
="@id/widget_img"
        android:id
="@+id/widget_time"
        android:layout_width
="match_parent"
        android:layout_height
="wrap_content"
        android:layout_weight
="1" />

</RelativeLayout>
appwidget-provider描述文件
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth
="300dip"
    android:minHeight
="20dip"
    android:updatePeriodMillis
="1000"
    android:initialLayout
="@layout/widgetlayout"
    
>
</appwidget-provider>
AndroidManifest.xml中的配置
<receiver 
            
android:name=".ShowTimeNowAppWidget"
            
>
            
<intent-filter>
                
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
                
<category android:name="android.intent.category.DEFAULT"/>
            
</intent-filter>
            
<meta-data android:name="android.appwidget.provider"
               android:resource
="@xml/widget" />
        
</receiver>
运行效果
posted on 2011-12-14 16:53 雪山飞鹄 阅读(1606) 评论(1)  编辑  收藏 所属分类: android

Feedback

# re: AppWidget之应用入门[实时更新系统当前时间] 2014-02-19 11:08 你的微笑百度搜不到
请问怎样使桌面上的日期格式随着设置中日期与格式动态改变而改变...  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: