編輯:關於 Android 程式設計
桌面小工具(App Widget)是 Android 相當有特色的一塊,它跟一般 Activity 完全不同:不能自由使用元件,只能透過 RemoteViews 更新畫面。本文以一個「輸入帳號後從叽歪(Plurk)抓取內容並顯示在桌面」的小工具為例,把整個開發步驟走一遍。
測試帳號:使用者 students、密碼 111111(僅供測試,請勿修改)。
新增一個類別並繼承 AppWidgetProvider。這個類別提供了 onDelete()、onEnable()、onDisabled() 等回呼,但實務上絕大多數情況只需要覆寫 onUpdate()。
在 onUpdate() 裡,通常會啟動背景執行緒或 Service 去抓資料,再把結果寫回 Widget。原因是 onUpdate() 跑在主執行緒上,網路請求不能直接在裡面做。
<receiver android:name="JiwaiWidget"
android:label="@string/app_name"
android:icon="@drawable/jiwai">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/info" />
</receiver>
逐行說明:
android:name:指向你的 AppWidgetProvider 子類,本例為 JiwaiWidget。android:label:小工具在選單上顯示的名稱,取自 values/string.xml 的 app_name。android:icon:小工具圖示,取自 drawable 下的 jiwai 圖片。intent-filter:必須接收 APPWIDGET_UPDATE 廣播,否則永遠不會收到更新通知。meta-data:指向描述檔,定義尺寸、更新間隔等資訊。
<appwidget-provider xmlns:android="https://schemas.android.com/apk/res/android"
android:minWidth="200dp"
android:minHeight="90dp"
android:updatePeriodMillis="43200000"
android:initialLayout="@layout/appwidget"
android:configure="com.lawrenst.jiwai.JiwaiConfigure">
</appwidget-provider>
各屬性含義:
minWidth / minHeight:小工具的最小尺寸,決定它能被放到多小的格子。updatePeriodMillis:系統自動更新的間隔(毫秒)。要注意系統對這個值有下限限制,寫太小不會真的那麼頻繁。initialLayout:小工具的版面配置檔。configure:可選。若小工具在加入桌面時需要先跳出 Activity 讓使用者設定,就指向該 Activity。本例因為要先輸入帳號密碼,所以必須設定。
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/widget"
android:background="@drawable/title_a">
<LinearLayout android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:background="@drawable/title">
<TextView android:id="@+id/username_display"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:textSize="15px"
android:gravity="left|center_vertical"
android:paddingLeft="6px" />
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/text1"
android:layout_width="fill_parent"
android:textColor="#ffffff"
android:textSize="12px"
android:gravity="center_vertical|left"
android:paddingLeft="6px"
android:layout_height="30px">
</TextView>
<TextView android:id="@+id/text2"
android:textColor="#ffffff"
android:layout_height="30px"
android:gravity="center_vertical|left"
android:textSize="12px"
android:paddingLeft="6px"
android:layout_width="fill_parent">
</TextView>
</LinearLayout>
</LinearLayout>
共三個 TextView:一個顯示使用者名稱,兩個顯示抓回來的叽歪內容。
這裡有個重要限制要牢記:Widget 版面不支援所有控制項,可用的是 FrameLayout、LinearLayout、RelativeLayout、GridLayout 這幾種 Layout,加上 AnalogClock、Button、Chronometer、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper 等少數元件。像 ListView 在早期版本就無法直接使用,自訂 View 也不行。
因為需要讓使用者輸入帳號密碼,必須額外做一個登入畫面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="#ff8c00"
android:capitalize="characters"
android:textStyle="bold" />
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user"
android:textColor="#ff8cff"
android:capitalize="characters" />
<EditText android:id="@+id/username"
android:layout_width="200px"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/code"
android:textColor="#ff8cff"
android:capitalize="characters" />
<EditText android:id="@+id/password"
android:layout_width="200px"
android:layout_height="wrap_content"
android:password="true" />
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
</LinearLayout>
兩個 EditText 分別接收帳號與密碼(密碼欄用 android:password="true" 遮罩輸入內容),加上一個送出用的 Button。
onUpdate(),在裡面啟動背景作業抓資料。res/xml/info.xml 描述尺寸、更新間隔與初始版面。configure 屬性中指向它。另外提醒:由於 Widget 的更新是透過 RemoteViews 操作,抓回來的資料要記得存到能跨程序讀寫的地方(例如 SharedPreferences 或 ContentProvider),並在 onUpdate() 裡用 AppWidgetManager.updateAppWidget() 推送到桌面。
本文原始作者:wenhuihhwh
Android 自定義垂直多色 SeekBar:漸層背景與觸摸回呼
垂直多色進度條的需求標準 SeekBar 多為水平,若需要垂直方向且具備多色漸層,通常需要自定義 View。核心做法是繼承 View,在 onDraw 中繪製背景色帶與滑
Android 仿微信聊天介面實作:文字訊息與語音錄製功能解析
仿微信聊天介面通常包含標題列、訊息列表、輸入框、語音切換按鈕與錄音提示層。設計重點在於讓文字輸入與語音輸入能自然切換,並提供清楚的錄音狀態回饋。 介面結構 聊天
Android 安全加密系列:消息摘要 Message Digest 完全指南
系列文章索引 Android 安全加密:對稱加密 Android 安全加密:非對稱加密 Android 安全加密:消息摘要 Message Digest Android
Android 開發筆記(一百零九):借網盤實現零成本雲存儲
一、為什麼要用網盤做存儲 個人開發者通常沒有自己的後台服務器,但又想收集 App 的運行信息、實現用戶數據同步,這時候第三方網絡存儲(網盤、雲盤、微盤)就是一個取巧的方案