天天看點

Android 添加App快捷方式到桌面

原創文章,如有轉載,請注明出處: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!

歡迎關注我的公衆号,和我一起每天進步一點點!

Android 添加App快捷方式到桌面