Android 添加、移除和判斷 桌面快捷方式圖標


思路:

  Launcher為了應用程序能夠定制自己的快捷圖標,就注冊了一個 BroadcastReceiver 專門接收其他應用程序發來的快捷圖標定制信息。所以只需要根據該 BroadcastReceiver 構造出相對應的Intent並裝入我們的定制信息,最后調用 sendBroadcast 方法就可以創建一個快捷圖標了。

 

步驟:

  1. 創建快捷方式必須要有權限;
  2. 創建快捷方式的廣播的 Intent 的 action 設置 com.android.launcher.action.INSTALL_SHORTCUT
  3. 刪除快捷方式的廣播的 Intent 的 action 設置 com.android.launcher.action.UNINSTALL_SHORTCUT
  4. 設置快捷方式的圖片和名稱等信息放在 Intent 中;

  需要添加的權限如下:

   <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"/>

  核心代碼為:

 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     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM