Android Widget學習筆記

來源:互聯網
上載者:User

首先記錄下基本的步驟吧

(1)總的來說就是修改三個XML,一個class...

(2)第一個xml是布局XML檔案(如:main.xml),是這個widget的。一般來說如果用這個組件顯示時間,那就只在這個布局XML中聲明一個textview就OK了

(3)第二個xml是widget_provider.xml,主要是用於聲明一個appwidget的 (其中:updatePeriodMillis是定時更新時間、每秒都會調用該 appwidget的onUpdate方法的作用、Layout就是指定上面那個main.xml)

(4)第三個是AndroidManifest.xml中,註冊broadcastReceiver資訊

(5)最後那個class用於做一些商務邏輯操作

具體的一些代碼

1、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"
    >
<TextView
android:id="@+id/tvCurrTime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    android:textColor="@color/black"
    />
</LinearLayout>

2、hello_widget_provider.xml部分

<?xml version="1.0" encoding="utf-8"?>
<!-- appwidget的updatePeriodMillis:定時更新時間、意思是每秒都會調用該 appwidget的onUpdate方法,onUpdate方法在兩種情況下被調用,第一種是添加appwidget時,第二種是每一個更新周期結束時調用一次onUpdate方法。註:此屬性在SDK1.5版本以後就失效了!!!如果需要更新的話必須自己寫個定時器哈,我的版本為2.2) -->
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:initialLayout="@layout/main">
</appwidget-provider>

 

3、AndroidManifest.xml部分

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.woody.testWidget"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
      <!-- HelloWidgetProvider為那個class(業務處理) -->

<receiver android:name=".HelloWidgetProvider" android:label="@string/app_name">
    <intent-filter>
    <!-- 指定了的 -->
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"

<!-- 為上面指定了的widget -->
    android:resource="@xml/hello_widget_provider" />
   </receiver>
    </application>
</manifest>

4、HelloWidgetProvider類的部分(進行時間的計算,核心onUpdate方法部分)

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
    int[] appWidgetIds) {
   Timer timer = new Timer();
   timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);
}

5、MyTime(自己寫的定時器)

public class MyTime extends TimerTask {

RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM,
Locale.getDefault());
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
thisWidget = new ComponentName(context, HelloWidgetProvider.class);
}
@Override
public void run() {
remoteViews.setTextViewText(R.id.tvCurrTime,
"Time = " + format.format(new Date()));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}

}

為了您的安全,請只開啟來源可靠的網址

開啟網站    取消

來自: http://hi.baidu.com/autumn%C0%B6%B8%F1%D7%D3/blog/item/f4ae54f5b1a94bff7609d746.html 從零開始建立一個Android主畫面Widgethttp://doc.chinaunix.net/android/200912/259645.shtml

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.