需要在Manifest里添加两个权限:
1 2
| <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
|
添加完就可以照搬下面的代码来实现了
添加Shortcut:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| Intent intent1 = new Intent("android.intent.action.MAIN");
intent1.setClass(MainActivity.this,MainActivity.class); intent1.addCategory("android.intent.category.LAUNCHER");
Intent intent2 = new Intent(); intent2.putExtra("android.intent.extra.shortcut.INTENT",intent1);
intent2.putExtra("android.intent.extra.shortcut.NAME","MainActivity");
intent2.putExtra("android.intent.extra.shortcut.ICON_RESOURCE",Intent.ShortcutIconResource.fromContext(MainActivity.this, R.drawable.icon));
intent2.putExtra("duplicate", false); intent2.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); sendOrderedBroadcast(intent2, null);
|
删除Shortcut:
1 2 3 4 5 6 7 8 9 10 11 12 13
| Intent intent1 = new Intent("android.intent.action.MAIN");
intent1.setClass(MainActivity.this,MainActivity.class); intent1.addCategory("android.intent.category.LAUNCHER");
Intent intent2 = new Intent(); intent2.putExtra("android.intent.extra.shortcut.INTENT",intent1);
intent2.putExtra("android.intent.extra.shortcut.NAME","MainActivity");
intent2.putExtra("android.intent.extra.shortcut.ICON_RESOURCE",Intent.ShortcutIconResource.fromContext(MainActivity.this, R.drawable.icon)); intent2.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); sendOrderedBroadcast(intent2, null);
|