建立,刪除快捷表徵圖shortcut android

來源:互聯網
上載者:User

在manifest.xml中,添加許可權:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

代碼如下:

private void uninstallShortcut(){ Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");    //com.android.launcher.action.UNINSTALL_SHORTCUT             //捷徑的名稱         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));         shortcut.putExtra("duplicate", false);     Intent intent = new Intent(Intent.ACTION_MAIN);    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        intent.setClass(getApplicationContext(), MainActivity.class);       shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);                      sendBroadcast(shortcut);     }private void installShortcut(){Log.i("CreateShortcutActivity","onclick to create shortcut");Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));shortcutIntent.putExtra("duplicate", false);Intent intent = new Intent(Intent.ACTION_MAIN);intent.setClass(getApplicationContext(), MainActivity.class);shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(CreateShortcutActivity.this,R.drawable.ic_shortcut));sendBroadcast(shortcutIntent);}

在android中,有InstallShortcutReceiver,和UninstallShortcutReceiver兩個類,用來接收該廣播。

注意:shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);

在InstallShortcutReceiver中,如果intent沒有設定action的話,會添加Intent.ACTION_VIEW,造成在Remove shortcut的時候,比較兩個Intent時,可能不一致而無法刪除該shortcut.

所以,建議要自己添加ACTION.

附上android Intent.filterEquals().就是這方法用來比較前後兩個Intent的,

  /**     * Determine if two intents are the same for the purposes of intent     * resolution (filtering). That is, if their action, data, type,     * class, and categories are the same.  This does <em>not</em> compare     * any extra data included in the intents.     *     * @param other The other Intent to compare against.     *     * @return Returns true if action, data, type, class, and categories     *         are the same.     */    public boolean filterEquals(Intent other) {        if (other == null) {            return false;        }        if (mAction != other.mAction) {            if (mAction != null) {                if (!mAction.equals(other.mAction)) {                    return false;                }            } else {                if (!other.mAction.equals(mAction)) {                    return false;                }            }        }        if (mData != other.mData) {            if (mData != null) {                if (!mData.equals(other.mData)) {                    return false;                }            } else {                if (!other.mData.equals(mData)) {                    return false;                }            }        }        if (mType != other.mType) {            if (mType != null) {                if (!mType.equals(other.mType)) {                    return false;                }            } else {                if (!other.mType.equals(mType)) {                    return false;                }            }        }        if (mPackage != other.mPackage) {            if (mPackage != null) {                if (!mPackage.equals(other.mPackage)) {                    return false;                }            } else {                if (!other.mPackage.equals(mPackage)) {                    return false;                }            }        }        if (mComponent != other.mComponent) {            if (mComponent != null) {                if (!mComponent.equals(other.mComponent)) {                    return false;                }            } else {                if (!other.mComponent.equals(mComponent)) {                    return false;                }            }        }        if (mCategories != other.mCategories) {            if (mCategories != null) {                if (!mCategories.equals(other.mCategories)) {                    return false;                }            } else {                if (!other.mCategories.equals(mCategories)) {                    return false;                }            }        }        return true;    }

可見,flag屬性是不在比較之列的,可以任意添加

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.