private void addShortcut(Context context,String name) {
Intent addShortcutIntent = new Intent(ACTION_ADD_SHORTCUT);
// 不允許重複建立
addShortcutIntent.putExtra("duplicate", false);// 經測試不是根據快捷方式的名字判斷重複的
// 應該是根據快鍊的Intent來判斷是否重複的,即Intent.EXTRA_SHORTCUT_INTENT字段的value
// 但是名稱不同時,雖然有的手機系統會顯示Toast提示重複,仍然會建立快鍊
// 螢幕上沒有空間時會提示
// 注意:重複建立的行為MIUI和三星手機上不太一樣,小米上似乎不能重複建立快捷方式
// 名字
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
// 圖示
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(context,
R.drawable.ic_launcher));
// 設定關聯程式
Intent sIntent = new Intent(Intent.ACTION_MAIN);
sIntent.addCategory(Intent.CATEGORY_LAUNCHER);// 加入action,和category之後,程式解除安裝的時候才會主動将該快捷方式也解除安裝
sIntent.setClass(this, DakaActivity.class);//點選後進入的Activity
addShortcutIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, sIntent);
// 發送廣播
sendBroadcast(addShortcutIntent);
}
在指定activity注冊時
android:exported="true">