厂家报了一个bug:清除数据后或重启手机后进入应用,就会在桌面上生成一个快捷方式,导致桌面有多个快捷方式显示。
但是公司出货了那么还只有这个厂家报告了这个问题。
看了代码后用来记录是否是第一次进入应用的方法为:
android.content.SharedPreferences.Editor editor = getEditor();
editor.putBoolean("isRunInFirstTime", isRunInFirstTime);
所以在清除数据后导致记录的不准确。
恰恰这个厂家的设置shortcut.putExtra("duplicate", false);是无效的,所以导致了这个问题。
在网上搜索了一下这个创建快捷方式的办法,依此记录
1、Android添加桌面快捷方式
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
/** * 为当前应用添加桌面快捷方式 * * @param cx * @param appName * 快捷方式名称 */ public static void addShortcut ( Context cx ) { Intent shortcut = new Intent ( "com.android.launcher.action.INSTALL_SHORTCUT" ) ; Intent shortcutIntent = cx. getPackageManager ( ) . getLaunchIntentForPackage (cx. getPackageName ( ) ) ; shortcut. putExtra (Intent. EXTRA_SHORTCUT_INTENT, shortcutIntent ) ; // 获取当前应用名称 String title = null ; try { final PackageManager pm = cx. getPackageManager ( ) ; title = pm. getApplicationLabel ( pm. getApplicationInfo (cx. getPackageName ( ), PackageManager. GET_META_DATA ) ). toString ( ) ; } catch ( Exception e ) { } // 快捷方式名称 shortcut. putExtra (Intent. EXTRA_SHORTCUT_NAME, title ) ; // 不允许重复创建(不一定有效) shortcut. putExtra ( "duplicate", false ) ; // 快捷方式的图标 Parcelable iconResource = Intent. ShortcutIconResource. fromContext (cx, R. drawable. ic_launcher ) ; shortcut. putExtra (Intent. EXTRA_SHORTCUT_ICON_RESOURCE, iconResource ) ; cx. sendBroadcast (shortcut ) ; } |
2、Android删除桌面快捷方式
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/** * 删除当前应用的桌面快捷方式 * * @param cx */ public static void delShortcut ( Context cx ) { Intent shortcut = new Intent ( "com.android.launcher.action.UNINSTALL_SHORTCUT" ) ; // 获取当前应用名称 String title = null ; try { final PackageManager pm = cx. getPackageManager ( ) ; title = pm. getApplicationLabel ( pm. getApplicationInfo (cx. getPackageName ( ), PackageManager. GET_META_DATA ) ). toString ( ) ; } catch ( Exception e ) { } // 快捷方式名称 shortcut. putExtra (Intent. EXTRA_SHORTCUT_NAME, title ) ; Intent shortcutIntent = cx. getPackageManager ( ) . getLaunchIntentForPackage (cx. getPackageName ( ) ) ; shortcut. putExtra (Intent. EXTRA_SHORTCUT_INTENT, shortcutIntent ) ; cx. sendBroadcast (shortcut ) ; } |
3、Android判断应用是否已存在桌面快捷方式
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
/** * 判断桌面是否已添加快捷方式 * * @param cx * @param titleName * 快捷方式名称 * @return */ public static boolean hasShortcut ( Context cx ) { boolean result = false ; // 获取当前应用名称 String title = null ; try { final PackageManager pm = cx. getPackageManager ( ) ; title = pm. getApplicationLabel ( pm. getApplicationInfo (cx. getPackageName ( ), PackageManager. GET_META_DATA ) ). toString ( ) ; } catch ( Exception e ) { } final String uriStr ; if (android. os. Build. VERSION. SDK_INT < 8 ) { uriStr = "content://com.android.launcher.settings/favorites?notify=true" ; } else { uriStr = "content://com.android.launcher2.settings/favorites?notify=true" ; } final Uri CONTENT_URI = Uri. parse (uriStr ) ; final Cursor c = cx. getContentResolver ( ). query (CONTENT_URI, null, "title=?", new String [ ] { title }, null ) ; if (c != null && c. getCount ( ) > 0 ) { result = true ; } return result ; } |
4、相关权限配置
1
2 3 |
<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" /> |
转载请注明地址: http://orgcent.com/android-add-del-shortcut-desktop/ | 萝卜白菜的博客