溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/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 雪山飛鵠 閱讀(1620) 評論(1)  編輯  收藏 所屬分類: android

          Feedback

          # re: AppWidget之應用入門[實時更新系統當前時間] 2014-02-19 11:08 你的微笑百度搜不到
          請問怎樣使桌面上的日期格式隨著設置中日期與格式動態改變而改變...  回復  更多評論
            

          主站蜘蛛池模板: 托克托县| 札达县| 蕲春县| 晋宁县| 扎鲁特旗| 河津市| 赣榆县| 博罗县| 和平区| 临夏县| 岳普湖县| 武定县| 邵阳县| 曲靖市| 青岛市| 扶绥县| 云浮市| 衢州市| 镇平县| 罗田县| 淳安县| 晋城| 皋兰县| 文登市| 镇巴县| 日喀则市| 肥城市| 元阳县| 砚山县| 安阳市| 天祝| 博白县| 盈江县| 永嘉县| 梧州市| 白河县| 夏津县| 方山县| 商南县| 九龙城区| 余庆县|