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

Android Theme:打造無介面的後台 Activity

編輯:關於 Android 程式設計

在 Android 開發中,我們偶爾會需要一些在後台默默執行的 Activity,但又不適合改用 Service。舉例來說,點擊程式圖示後直接導向指定網址,完全不顯示任何程式介面,這種情境就很適合用特殊的 Theme 來實現。

此時可以在 AndroidManifest.xml 中,為對應的 Activity 標籤加上下面這行屬性:

android:theme="@android:style/Theme.NoDisplay"

接著在該 Activity 中於 onPause() 裡呼叫 finish(),讓它執行完就立刻關閉:

@Override
protected void onPause() {
    super.onPause();
    finish();
}

補充:開啟指定網址的兩種寫法

指定網址並使用特定瀏覽器開啟:

Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("https://www.baidu.com");
intent.setData(content_url);
intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
startActivity(intent);

指定網址並交由預設瀏覽器開啟:

Intent intent = new Intent(Intent.ACTION_VIEW,
        Uri.parse("https://www.example.com/main.html"));
startActivity(intent);

常見的內建 Theme 一覽

Android 提供了豐富的內建主題,可以應付各種介面需求:

・android:theme="@android:style/Theme.Dialog" 將 Activity 顯示為對話框模式
・android:theme="@android:style/Theme.NoTitleBar" 不顯示標題欄
・android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 不顯示標題欄並全螢幕
・android:theme="Theme.Light" 白色背景
・android:theme="Theme.Light.NoTitleBar" 白色背景且無標題欄
・android:theme="Theme.Light.NoTitleBar.Fullscreen" 白色背景、無標題欄、全螢幕
・android:theme="Theme.Black" 黑色背景
・android:theme="Theme.Black.NoTitleBar" 黑色背景且無標題欄
・android:theme="Theme.Black.NoTitleBar.Fullscreen" 黑色背景、無標題欄、全螢幕
・android:theme="Theme.Wallpaper" 以系統桌布作為背景
・android:theme="Theme.Wallpaper.NoTitleBar" 系統桌布背景且無標題欄
・android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" 系統桌布背景、無標題欄、全螢幕
・android:theme="Translucent" 半透明主題
・android:theme="Theme.Translucent.NoTitleBar" 半透明且無標題欄
・android:theme="Theme.Translucent.NoTitleBar.Fullscreen" 半透明、無標題欄、全螢幕
・android:theme="Theme.Panel" 面板風格主題
・android:theme="Theme.Light.Panel" 淺色面板風格主題

善用這些預設主題,往往能省去大量自訂樣式的工夫。

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