Android案頭組件App Widget開發三步走

來源:互聯網
上載者:User

案頭組件App Widget是Android的實用功能,開發過程雖然不是很難,但是步驟不少,略有麻煩。為了方便以後再次使用的時候,快速上手,概括了下面的關鍵步驟。並且把項目打了包,方便以後的使用。建立一個Android項目,按已下三步就可以製作一個簡單App Widget。

Step 1 建立AppWidgetProvider子類

public class SimpleWidget extends AppWidgetProvider{    @Override    public void onUpdate(Context context,AppWidgetManager appWidgetManager,        int[] appWidgetIds){        super.onUpdate(context, appWidgetManager, appWidgetIds);    }}

Step 2 建立App Widget布局檔案和設定檔案

布局檔案 res/layout/simple_widget.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView" /></LinearLayout>

設定檔案 res/xml/simple_widget.xml

(initialLayout要和布局檔案名稱匹配)

<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"     android:initialLayout="@layout/simple_widget"     android:minWidth="72dp"     android:minHeight="72dp"     android:updatePeriodMillis="86400000"     ></appwidget-provider>

Step 3 修改AndroidManifest.xml

在Application節點下增加下面的子節點:

<receiver android:name="SimpleWidget" >    <intent-filter>        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />    </intent-filter>    <meta-data        android:name="android.appwidget.provider"        android:resource="@xml/simple_widget" /></receiver>

全部儲存,到這一步就可以開始Run了。示範:

最後給出,打包的項目原始碼包,。

相關文章

聯繫我們

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