Android應用添加(建立)和刪除及判斷是否存在案頭捷徑

來源:互聯網
上載者:User

標籤:

Android應用添加(建立)和刪除及判斷是否存在案頭捷徑-Android新手入門-eoe 移動開發人員論壇 - Powered by Discuz!

Android傳統型程式提供了應用添加和刪除案頭捷徑的功能以及判斷捷徑是否存在,

只要傳入捷徑標題、表徵圖及點擊捷徑執行的應用Intent即可。代碼如下:



1、Android添加案頭捷徑



/**

* 為當前應用添加案頭捷徑

*

* @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刪除案頭捷徑



/**

* 刪除當前應用的案頭捷徑

*

* @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判斷應用是否已存在案頭捷徑




/**

* 判斷案頭是否已添加捷徑

*

* @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、相關許可權配置





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

Android應用添加(建立)和刪除及判斷是否存在案頭捷徑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.