Android桌面 快捷方式的删除

本文详细介绍了快捷方式的删除规则,通过Intent.EXTRA_SHORTCUT_NAME和intent属性的比对来实现快捷方式的查找与删除。重点展示了UninstallShortcutReceiver类中removeShortcut方法的工作原理,包括使用filterEquals方法来精确匹配意图,以及对共享偏好设置的更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

快捷方式的删除规则:

首先根据Intent.EXTRA_SHORTCUT_NAME来查找,

然后再比对intent是否一致(Action、data、type、package、component、categories)

比对正确则删除;

UninstallShortcutReceiver.java

    private static void removeShortcut(Context context, Intent data, final SharedPreferences sharedPrefs) {
        Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
        String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
        boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);

        if (intent != null && name != null) {
            final ContentResolver cr = context.getContentResolver();
            Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
                new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.INTENT },
                <span style="color:#FF0000;">LauncherSettings.Favorites.TITLE + "=?", new String[] { name }</span>, null);

            final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
            final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);

            boolean changed = false;

            try {
                while (c.moveToNext()) {
                    try {
                        if (intent.filterEquals(Intent.parseUri(c.getString(intentIndex), 0))) {
                            final long id = c.getLong(idIndex);
                            final Uri uri = LauncherSettings.Favorites.getContentUri(id, false);
                            cr.delete(uri, null, null);
                            changed = true;
                            if (!duplicate) {
                                break;
                            }
                        }
                    } catch (URISyntaxException e) {
                        // Ignore
                    }
                }
            } finally {
                c.close();
            }

            if (changed) {
                cr.notifyChange(LauncherSettings.Favorites.CONTENT_URI, null);
                Toast.makeText(context, context.getString(R.string.shortcut_uninstalled, name),
                        Toast.LENGTH_SHORT).show();
            }

            // Remove any items due to be animated
            boolean appRemoved;
            Set<String> newApps = new HashSet<String>();
            newApps = sharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
            synchronized (newApps) {
                do {
                    appRemoved = newApps.remove(intent.toUri(0).toString());
                } while (appRemoved);
            }
            if (appRemoved) {
                final Set<String> savedNewApps = newApps;
                new Thread("setNewAppsThread-remove") {
                    public void run() {
                        SharedPreferences.Editor editor = sharedPrefs.edit();
                        editor.putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY,
                                savedNewApps);
                        if (savedNewApps.isEmpty()) {
                            // Reset the page index if there are no more items
                            editor.putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1);
                        }
                        editor.commit();
                    }
                }.start();
            }
        }
    }
intent.filterEquals

/**
     * 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;
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值