原創文章,如有轉載,請注明出處:http://blog.csdn.net/myth13141314/article/details/68926849
主要原理是通過向系統發送建立快捷方式的廣播
- 設定Intent,傳遞快捷方式的資訊,名字和圖示等
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//快捷方式的名稱
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getResources().getString(R.string.app_name));//快捷方式的名字
shortcut.putExtra("duplicate", false); //不允許重複建立
//快捷方式的圖示
ShortcutIconResource iconRes = ShortcutIconResource.fromContext(context, R.mipmap.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
- 用于發送建立快捷方式的廣播
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(context, context.getClass().getName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
- 發送建立快捷方式的廣播
需要申請權限,否則建立不了
<!-- 快捷方式 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
以上簡單幾步,快捷方式建立OK!
歡迎關注我的公衆号,和我一起每天進步一點點!
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIwczLcVmds92czlGZvwVP9EUTDZ0aRJkSwk0LcxGbpZ2LcBDM08CXlpXazRnbvZ2LcRlMMVDT2EWNvwFdu9mZvwVP9EkT41EVNBTR61EendEZ1EjMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2LcRHelR3LcJzLctmch1mclRXY39TM5IDN1MTNzETMzcDM4EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)