android widget–巴西世界盃倒計時實現?

來源:互聯網
上載者:User

【博主:各位博友,網友們,大家網上好!歡迎光臨本部落格。

歡迎多多交流,多提意見,互相學習,互相進步,我們的口號是:好好學習,天天向上。】


最近看了不少資料,關於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.

 

介紹完了,希望給給大家協助。

 

 

轉載請註明出處。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.