引用地址:http://mobile.51cto.com/widget-290079.htm
ndroid Widget開發案例實現是本文要介紹的內容,主要是來瞭解並學習Android Widget開發應用,今天我們要寫一下Android Widget的開發,由於快點淩晨,我就不說的太具體了,同志們就模仿吧!首先看一下:
下面是Demo的詳細步驟:
一、建立一個Android工程命名為:WidgetDemo.
二、準備素材,一個是Widget的表徵圖,一個是Widget的背景。存放目錄如:
三、修改string.xml檔案如下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, WidetDemo!</string>
- <string name="app_name">DaysToWorldCup</string>
- </resources>
四、建立Widget內容提供者檔案,我們在res下建立xml檔案夾,並且建立一個widget_provider.xml代碼入下:
- <?xml version="1.0" encoding="utf-8"?>
- <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
- android:minWidth="50dip"
- android:minHeight="50dip"
- android:updatePeriodMillis="10000"
- android:initialLayout="@layout/main"
- />
五、修改main.xml布局,代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/wordcup"
- >
- <TextView
- android:id="@+id/wordcup"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- android:textSize="12px"
- android:textColor="#ff0000"
- />
- </LinearLayout>
六、修改WidgetDemo.java代碼如下:
- package com.android.tutor;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.appwidget.AppWidgetManager;
- import android.appwidget.AppWidgetProvider;
- import android.content.ComponentName;
- import android.content.Context;
- import android.widget.RemoteViews;
- public class WidetDemo extends AppWidgetProvider {
- /** Called when the activity is first created. */
-
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int[] appWidgetIds) {
-
- Timer timer = new Timer();
- timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), 1, 60000);
- super.onUpdate(context, appWidgetManager, appWidgetIds);
- }
-
- private class MyTime extends TimerTask{
- RemoteViews remoteViews;
- AppWidgetManager appWidgetManager;
- ComponentName thisWidget;
-
- public MyTime(Context context,AppWidgetManager appWidgetManager){
- this.appWidgetManager = appWidgetManager;
- remoteViews = new RemoteViews(context.getPackageName(),R.layout.main);
-
- thisWidget = new ComponentName(context,WidetDemo.class);
- }
- public void run() {
-
- Date date = new Date();
- Calendar calendar = new GregorianCalendar(2010,06,11);
- long days = (((calendar.getTimeInMillis()-date.getTime())/1000))/86400;
- remoteViews.setTextViewText(R.id.wordcup, "距離南非世界盃還有" + days+"天");
- appWidgetManager.updateAppWidget(thisWidget, remoteViews);
-
- }
- }
- }
七、修改設定檔AndroidManifest.xml,代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.tutor"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <receiver android:name=".WidetDemo"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
- </intent-filter>
- <meta-data android:name="android.appwidget.provider"
- android:resource="@xml/widget_provider"
- />
- </receiver>
- </application>
- <uses-sdk android:minSdkVersion="7" />
- </manifest>
八、點擊運行(Ctrl+F11),之,運行成功後,我們長時間點擊案頭,會出現如下倆個,依次點擊,就可以看到最上面的:
小結:解析Android Widget開發案例實現的內容介紹完了,希望通過Android Widget開發內容的學習能對你有所協助!
本文完。