Android Widget開(kāi)發(fā) 案例實(shí)現(xiàn)是本文要介紹的內(nèi)容,主要是來(lái)了解并學(xué)習(xí) Android Widget開(kāi)發(fā) 應(yīng)用,今天我們要寫一下 Android Widget 的 開(kāi)發(fā) ,由于快點(diǎn)凌晨,我就不說(shuō)的太具體了,同志們就模仿吧!首先看一下效果圖:
下面是Demo的詳細(xì)步驟:
一、新建一個(gè)Android工程命名為:WidgetDemo.
二、準(zhǔn)備素材,一個(gè)是Widget的圖標(biāo),一個(gè)是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 內(nèi)容提供者文件,我們?cè)趓es下建立xml文件夾,并且新建一個(gè)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"/>
其中寬度、長(zhǎng)度很清楚,還有android:updatePeriodMillis是自動(dòng)更新的時(shí)間間隔,android:initialLayout是Widget的界面描述文件。
還有一個(gè)屬性Android:configure是可選的,如果你的Widget需要在啟動(dòng)時(shí)先啟動(dòng)一個(gè)Activity,則需要設(shè)定該項(xiàng)為你的Activity。
五、修改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>
其中
<receiver android:name=".WidetDemo" android:label="@string/app_name">
name指定該Widget的接收者是WidetDemo,即你建立的AppWidgetProvider子類,label指定該Widget的標(biāo)簽,還可以用屬性icon指定圖標(biāo)
<intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter>
是采用android文檔中提供的,用于接收更新的intent意圖
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider"/>
resource指定該Widget的描述信息,該描述中定義了Widget的相關(guān)信息,如該Widget的寬度、長(zhǎng)度、自動(dòng)更新的間隔時(shí)間等信息,也就是前面四所定義的內(nèi)容
八、點(diǎn)擊運(yùn)行(Ctrl+F11),之,運(yùn)行成功后,我們長(zhǎng)時(shí)間點(diǎn)擊桌面,會(huì)出現(xiàn)如下倆個(gè),依次點(diǎn)擊,就可以看到最上面的效果圖:
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
