1、在桌面添加快捷方式,在Activity添加如下代码
private final String ACTION_ADD_SHORTCUT =
“com.android.launcher.action.INSTALL_SHORTCUT”;
Intent addShortcut =new Intent(ACTION_ADD_SHORTCUT);
String appame = getResources().getString(R.String.app_name);
Parcelable appicon = Intent.ShortcutIconResource.fromContext(this,R.drawable.icon);
//图标
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,appicon);
//名称
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,appname);
//启动程序Intent
PackageManager packageManager = this.getPackageManager();
Intent openIntent = packageManager.getLaunchIntentForPackage("包名");
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,openIntent);
sendBroadcast(addShortcut);
需要添加权限:com.android.launcher.permission.INSTALL_SHORTCUT
2、在长按桌面弹出的菜单的快捷方式中显示应用:
在AndroidManifest.xml中得启动Activity中添加:
<intent-filter>
<action android:name="android.intent.action.CREATE_SHUTCUT" />
</intent-filter>