Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 基於 IIS 的 APK 下載(一):自定義更新控件

編輯:關於 Android 程式設計

Android 越來越普及,那已安裝的應用要如何更新呢?應用市場裡常會顯示「某某應用已更新」之類的資訊,我們能否也實現類似功能?要做哪些準備?做過 Web 開發的人都知道,Web 伺服器種類很多,如 Windows 自帶的 IIS、跨平台的 Apache 等。這裡以 IIS 作為伺服器來實現 APK 的更新下載,先來看效果圖。

可以看到,取得更新列表後可根據情況進行更新,下載完成後進行安裝,這樣就完成了一次更新。第一步,我們先自定義更新列表中每一行的控件。

一、update_item.xml(單行佈局)

<?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="64px"
    android:paddingLeft="10px"
    android:paddingTop="10px"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/update_item_app_image"
        android:layout_width="64px"
        android:layout_height="64px"
        android:background="@drawable/apk">
    </ImageView>
    <TextView
        android:id="@+id/update_item_app_name"
        android:text="TestApk">
    </TextView>
    <TextView
        android:id="@+id/update_item_app_old_version"
        android:gravity="right"
        android:text="Not Install">
    </TextView>
    <TextView
        android:layout_width="50px"
        android:gravity="center"
        android:text="->">
    </TextView>
    <TextView
        android:id="@+id/update_item_new_version"
        android:gravity="left"
        android:text="1.0.0.0">
    </TextView>
    <Button
        android:id="@+id/update_item_behavior"
        android:text="Update">
    </Button>
</LinearLayout>

說明:

  1. update_item_app_image 使用了一個名為 apk 的背景圖,可自行更換。
  2. 這裡借用 HTML + CSS 的思路,盡可能把佈局與樣式分離,透過 style 引用樣式(見下方代碼)。

二、update_item_style.xml(樣式)

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="https://schemas.android.com/apk/res/android">
    <style name="update_item_textview">
        <item name="android:layout_width">180px</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginLeft">20px</item>
        <item name="android:textColor">#000000</item>
        <item name="android:textSize">30sp</item>
    </style>
    <style name="update_item_button">
        <item name="android:layout_width">180px</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginLeft">20px</item>
        <item name="android:textColor">#000000</item>
        <item name="android:textSize">30sp</item>
    </style>
</resources>

說明:

  1. 該 xml 檔放在 values 中,與 update_item.xml 相對應。
  2. 為便於查閱與維護,命名採用「功能 + 作用」的形式,均為小寫,不同單詞間以下底線連接。例如 style 對應 update_item,故命名為 update_item_style.xml;佈局檔則以功能命名,即 update_item.xml。佈局中的 ID 與 style 的 name 也採用類似命名法。
  3. 佈局檔中的字串最好也有對應的檔案,如 update_item_strings.xml,這裡僅作示例未單獨給出。

自定義好更新控件後就能使用了。使用時,要先找到這個佈局,再添加到相應位置。從效果圖看,更新列表是按行分布的,可用多種方式實現,如 RelativeLayout + LinearLayout、ListView + Adapter 等,這裡採用 ListView + Adapter。具體做法請參見〈Android 基於 IIS 的 APK 下載(二)顯示更新列表〉。完整程式碼可於作者的 GitHub 專案 ApkUpdate 取得。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved