現在的位置: 首頁 > 綜合 > 正文

Android應用添加(創建)和刪除及判斷是否存在桌面快捷方式

2013年10月04日 ⁄ 綜合 ⁄ 共 1839字 ⁄ 字號 評論關閉

    廠家報了一個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) {

抱歉!評論已關閉.