您的位置:首页 > 移动开发 > Android开发

Android桌面 快捷方式的删除

2014-11-04 16:22 351 查看
快捷方式的删除规则:

首先根据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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: