1、判断是否存在快捷方式:
title = ? 查询条件指的是创建快捷方式时设置的label字符串(Intent.EXTRA_SHORTCUT_NAME)
private boolean hasAddShortCut() { boolean isInstallShortcut = false; final ContentResolver cr = this.getContentResolver(); String AUTHORITY = "com.android.launcher2.settings"; final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); Cursor c = cr.query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?", new String[] { getString(R.string.app_name) }, null); if (c != null && c.getCount() > 0) { isInstallShortcut = true; } return isInstallShortcut; }
2、创建快捷方式
private void addShortCut(){ Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name)); //设置是否可重复创建 shortcut.putExtra("duplicate", false); //设置显示的图标 Parcelable icon = Intent.ShortcutIconResource.fromContext(this,R.mipmap.ic_launcher_mail); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon); //点击快捷方式发起的intent的定义 Intent intent = new Intent(Intent.ACTION_MAIN); intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY); intent.addCategory(Intent.CATEGORY_LAUNCHER); //对于使用activity-alias控件的请用setClassName接口来设置启动的activity</span> //intent.setClass(MailActivityEmail.this, MailActivityEmail.class); intent.setClassName("com.kingsoft.email","com.kingsoft.email.activity.Welcome"); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); this.sendBroadcast(shortcut); }