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

Android 設定壁紙與桌面圖示

編輯:關於 Android 程式設計

設定壁紙

設定壁紙可先將 Drawable 轉為 Bitmap,再呼叫 setWallpaper。

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper);
try {
    setWallpaper(bitmap);
} catch (IOException e) {
    e.printStackTrace();
}

建立桌面圖示

可送出 INSTALL_SHORTCUT 廣播,指定名稱、圖示與啟動 Intent。

Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
shortcut.putExtra("duplicate", false);
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(this, this.getClass().getName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
sendBroadcast(shortcut);

刪除桌面圖示

可送出 UNINSTALL_SHORTCUT 廣播,並指定要刪除的圖示名稱與 Intent。

檢查是否已存在

可查詢 Launcher 的 favorites 資料表,以應用名稱判斷是否已有圖示。若已存在,可提示使用者或直接跳過建立。

注意事項

不同 Launcher 對桌面圖示的支援程度不同。部分系統可能需要額外權限或使用者手動允許。

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