【博主:各位博友,網友們,大家網上好!歡迎光臨本部落格。
歡迎多多交流,多提意見,互相學習,互相進步,我們的口號是:好好學習,天天向上。】
最近看了不少資料,關於widget的東東還真不少。自己做個DEMO並做下總結。
widget含義:基實就是視窗小組件的意思。就是手機首頁上的一些小程式。例如:倒計時,音樂播放控制器,目前時間的顯示等等。應用很廣。
接下來建立自己的第一個widget小程式:
建立工程:WidgetDemo
widget.xml:
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:background="@drawable/widget"<br /> ><br /><TextView<br /> android:id="@+id/wordcup"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="@string/hello"<br /> android:textSize="12px"<br /> android:textColor="#ff0000"<br /> /><br /></LinearLayout>
建立res/xml/widget_provider.xml
<?xml version="1.0" encoding="utf-8"?><br /><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"<br />android:minWidth="60dip" android:minHeight="60dip"<br />android:updatePeriodMillis="10000" android:initialLayout="@layout/widget"/>
配置AndroidManifest.xml,註冊廣播資訊:
<receiver android:name=".WidgetDemo" android:label="@string/app_name"><br /><intent-filter><br /><action android:name="android.appwidget.action.APPWIDGET_UPDATE" /><br /></intent-filter><br /><meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" /><br /></receiver>
WidgetDemo.java建立,處理商務邏輯:
import java.util.Calendar;<br />import java.util.Date;<br />import java.util.GregorianCalendar;<br />import java.util.Timer;<br />import java.util.TimerTask;<br />import android.appwidget.AppWidgetManager;<br />import android.appwidget.AppWidgetProvider;<br />import android.content.ComponentName;<br />import android.content.Context;<br />import android.widget.RemoteViews;<br />public class WidgetDemo extends AppWidgetProvider{</p><p>/**<br /> * 更新widget<br /> */<br />@Override<br /> public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {<br /> Timer timer = new Timer();<br /> timer.scheduleAtFixedRate(new ATime(context,appWidgetManager), 1, 60000);<br /> super.onUpdate(context, appWidgetManager, appWidgetIds);<br /> } </p><p> /**<br /> * 定時器實現<br /> *<br /> */<br /> private class ATime extends TimerTask{<br /> RemoteViews remoteViews;<br /> AppWidgetManager appWidgetManager;<br /> ComponentName thisWidget; </p><p> Date date = new Date();<br /> Calendar calendar = new GregorianCalendar(2014,06,13);<br /> long days = (((calendar.getTimeInMillis()-date.getTime())/1000))/86400; </p><p> public ATime(Context context,AppWidgetManager appWidgetManager){<br /> this.appWidgetManager = appWidgetManager;<br /> remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget1); </p><p> thisWidget = new ComponentName(context,WidgetDemo.class);<br /> }<br /> public void run() {<br /> remoteViews.setTextViewText(R.id.wordcup, "距離巴西世界盃還有" + days+"天");<br /> appWidgetManager.updateAppWidget(thisWidget, remoteViews);<br /> }<br /> }<br />}
最後效果:
在手機主介面,長按,然後點widget,選擇wdigetDemo,ok.
介紹完了,希望給給大家協助。
轉載請註明出處。