<span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">下面的方式是用来判断是否包含相应标题名称的桌面快捷方式。</span>
public boolean hasShortCut(String title)
{
String url = "";
url = "content://" + getAuthorityFromPermission(this, READ_SETTINGS)
+ "/favorites?notify=true";
// 获取当前应用名称
// Log.i("url:", url);
// try {
// final PackageManager pm = getPackageManager();
// title = pm.getApplicationLabel(
// pm.getApplicationInfo(getPackageName(),
// PackageManager.GET_META_DATA)).toString();
// } catch (Exception e) {
// }
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(Uri.parse(url), null, "title=?",
new String[] { title }, null);
if (cursor != null && cursor.getCount() > 0)
{
cursor.close();
return true;
}
return false;
}
添加删除桌面快捷方式
/**
* 添加快捷方式
* */
public void creatShortCut(String shortcutName, int resourceId)
{
Intent intent = new Intent();
intent.setClass(this, LaunchActivity.class);
// intent.setClass(this, LaunchActivity.class);
/* 以下两句是为了在卸载应用的时候同时删除桌面快捷方式 */
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
Intent shortcutintent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 不允许重复创建
shortcutintent.putExtra("duplicate", false);
// 需要显示的名称
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
// 快捷图片
Parcelable icon = Intent.ShortcutIconResource.fromContext(
getApplicationContext(), resourceId);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
// ComponentName comp = new ComponentName(this.getPackageName(),
// this.getPackageName() + "." + this.getLocalClassName());
// shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
// Intent.ACTION_MAIN).setComponent(comp));
// 点击快捷图片,运行的程序主入口
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
// 发送广播。OK
sendBroadcast(shortcutintent);
}
/**
* 删除程序的快捷方式
*/
private void delShortcut(String shortcutName)
{
Intent shortcut = new Intent(
"com.android.launcher.action.UNINSTALL_SHORTCUT");
// 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
// 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
// 注意: ComponentName的第二个参数必须是完整的类名(包名+类名),否则无法删除快捷方式
String appClass = this.getPackageName() + "."
+ this.getLocalClassName();
ComponentName comp = new ComponentName(this.getPackageName(), appClass);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
Intent.ACTION_MAIN).setComponent(comp));
sendBroadcast(shortcut);
}