思路:android
Launcher爲了應用程序可以定製本身的快捷圖標,就註冊了一個 BroadcastReceiver 專門接收其餘應用程序發來的快捷圖標定製信息。因此只須要根據該 BroadcastReceiver 構造出相對應的Intent並裝入咱們的定製信息,最後調用 sendBroadcast 方法就能夠建立一個快捷圖標了。app
步驟:ui
須要添加的權限以下:spa
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher2.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS"/>
核心代碼爲:code
1 /**
2 * 添加當前應用的桌面快捷方式 3 * 4 * @param context 5 */
6 public static void addShortcut(Context context, int appIcon) { 7 Intent shortcut = new Intent( 8 "com.android.launcher.action.INSTALL_SHORTCUT"); 9
10 Intent shortcutIntent = context.getPackageManager() 11 .getLaunchIntentForPackage(context.getPackageName()); 12 shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 13 // 獲取當前應用名稱
14 String title = null; 15 try { 16 final PackageManager pm = context.getPackageManager(); 17 title = pm.getApplicationLabel( 18 pm.getApplicationInfo(context.getPackageName(), 19 PackageManager.GET_META_DATA)).toString(); 20 } catch (Exception e) { 21 } 22 // 快捷方式名稱
23 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); 24 // 不容許重複建立(不必定有效)
25 shortcut.putExtra("duplicate", false); 26 // 快捷方式的圖標
27 Parcelable iconResource = Intent.ShortcutIconResource.fromContext(context, 28 appIcon); 29 shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); 30
31 context.sendBroadcast(shortcut); 32 } 33
34 /**
35 * 刪除當前應用的桌面快捷方式 36 * 37 * @param context 38 */
39 public static void delShortcut(Context context) { 40 Intent shortcut = new Intent( 41 "com.android.launcher.action.UNINSTALL_SHORTCUT"); 42
43 // 獲取當前應用名稱
44 String title = null; 45 try { 46 final PackageManager pm = context.getPackageManager(); 47 title = pm.getApplicationLabel( 48 pm.getApplicationInfo(context.getPackageName(), 49 PackageManager.GET_META_DATA)).toString(); 50 } catch (Exception e) { 51 } 52 // 快捷方式名稱
53 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); 54 Intent shortcutIntent = context.getPackageManager() 55 .getLaunchIntentForPackage(context.getPackageName()); 56 shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 57 context.sendBroadcast(shortcut); 58 } 59
60 /**
61 * 判斷當前應用在桌面是否有桌面快捷方式 62 * 63 * @param context 64 */
65 public static boolean hasShortcut(Context context) { 66 boolean result = false; 67 String title = null; 68 try { 69 final PackageManager pm = context.getPackageManager(); 70 title = pm.getApplicationLabel( 71 pm.getApplicationInfo(context.getPackageName(), 72 PackageManager.GET_META_DATA)).toString(); 73 } catch (Exception e) { 74
75 } 76
77 final String uriStr; 78 if (android.os.Build.VERSION.SDK_INT < 8) { 79 uriStr = "content://com.android.launcher.settings/favorites?notify=true"; 80 } else if (android.os.Build.VERSION.SDK_INT < 19) { 81 uriStr = "content://com.android.launcher2.settings/favorites?notify=true"; 82 } else { 83 uriStr = "content://com.android.launcher3.settings/favorites?notify=true"; 84 } 85 final Uri CONTENT_URI = Uri.parse(uriStr); 86 final Cursor c = context.getContentResolver().query(CONTENT_URI, null, 87 "title=?", new String[]{title}, null); 88 if (c != null && c.getCount() > 0) { 89 result = true; 90 } 91 return result; 92 }