編輯:關於 Android 程式設計
想讓使用者在桌面(Launcher)上看到你的 App 捷徑,其實只要三步:
1. 建立一個 Intent,其 Action 為 com.android.launcher.action.INSTALL_SHORTCUT。
2. 透過該 Intent 的 Extra 屬性設定捷徑的名稱、圖示,以及點擊後要啟動的程式。
3. 呼叫 sendBroadcast() 送出廣播,捷徑就會出現在桌面。
下面用一個簡單範例演示。為了避免每次開啟 App 都重複建立捷徑,這裡用 SharedPreferences 記一個計數器:第一次安裝時建立捷徑,之後就不再建立。
package com.home.activity;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.Toast;
import com.example.testshortcut.R;
public class TestShortcutActivity extends Activity {
private SharedPreferences sp;
private Editor editor;
private int count = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp = this.getSharedPreferences("testshortcut", MODE_PRIVATE);
editor = sp.edit();
// 讀取先前記錄的計數
int readCount = sp.getInt("count", 0);
if (readCount > 0) {
Toast.makeText(this, "捷徑已存在,不必再建立", Toast.LENGTH_LONG).show();
return;
}
// 建立「增加捷徑」的 Intent
Intent addIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 捷徑名稱
String title = "我的應用";
// 載入捷徑圖示
Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
// 點擊捷徑後要啟動的程式(這裡啟動自己)
Intent myIntent = new Intent(this, TestShortcutActivity.class);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);
// 送出廣播
sendBroadcast(addIntent);
// 記錄已建立過
editor.putInt("count", count);
editor.commit();
}
}
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
除了 MAIN/LAUNCHER 這個標準的啟動入口之外,還要多加一個 CREATE_SHORTCUT 的 intent-filter,讓 Launcher 知道這個 App 允許被建立捷徑。
<activity
android:name="com.home.activity.TestShortcutActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- 定義可加入桌面 Launcher -->
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter>
</activity>
用 SharedPreferences 判斷是否已建立並不可靠。使用者完全可以自己把捷徑刪掉,此時 App 內的計數仍是 1,就會永遠不再補建。若真的需要確保捷徑存在,比較好的做法是提供一個明確的「加入桌面」按鈕,讓使用者自行觸發。
各家的 Launcher 支援度不一。INSTALL_SHORTCUT 屬於非官方(未列入正式 API 文件)的約定,部分客製化 Launcher 會忽略這個廣播,因此務必搭配 try-catch 或至少不要依賴它作為唯一入口。
如果要發布到 Google Play,建議改用官方 API。Android 8.0(API 26)之後提供了 requestPinShortcut(),會由系統跳出標準的「加到主畫面」對話框,行為一致性與使用者告知都更好:
ShortcutManager sm = getSystemService(ShortcutManager.class);
if (sm.isRequestPinShortcutSupported()) {
ShortcutInfo info = new ShortcutInfo.Builder(this, "my_shortcut")
.setShortLabel("我的應用")
.setIcon(Icon.createWithResource(this, R.drawable.ic_launcher))
.setIntent(new Intent(this, TestShortcutActivity.class))
.build();
sm.requestPinShortcut(info, null);
}
相反的,若要移除自己建立的動態捷徑,可用 removeDynamicShortcuts();而 com.android.launcher.permission.UNINSTALL_SHORTCUT 這個權限實際上並不受系統保護,任何 App 都能移除捷徑,這一點在評估捷徑存在性時要特別注意。
Android Studio 開發筆記——關於編譯的那些常見錯誤
在 Android Studio 的日常開發裡,不管是初次同步新專案,還是接手別人留下的舊代碼,最常撞上的攔路虎之一,就是「找不到 Android Support Lib
Android 多執行緒下載實作:帶進度條與百分比的完整範例
本文分享一個在 Android 上實作「多執行緒下載並顯示進度條與百分比」的完整範例,供需要的朋友參考。 延續之前〈Java 多執行緒下載實例詳解〉的思路,我們可以把同樣
RecyclerView 多種 Item 佈局實作:Header + Body + Footer 一次搞懂
RecyclerView 要在一個列表裡混搭多種樣式,關鍵是 getItemViewType()。只要把位置映射成型別,剩下的交給 onCreateViewHolder(
Android 圖片特效實作:黑白、圓角與高斯模糊
在 Android 中對圖片做特效處理是很實用的技巧。本文整理三種常見效果的實作:黑白化、圓角與高斯模糊。 一、黑白效果 將彩色圖轉為黑白圖的核心,是把每個像素的 RG