在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); }
注意: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; }