? ??Android在桌面上生成快捷方式有两种情况,一种是直接在桌面直接生成;一种是长按桌面,在弹出的快捷菜单中生成。这是讨论第一种,直接在桌面生成。
? ??这种是通过广播(Broadcast)的形式向Luncher发送请求生成快捷方式的。
首先在activity中设置:
<receiver android:name="com.android.launcher2.InstallShortcutReceiver" android:permission="com.android.launcher.permission.INSTALL_SHORTCUT" > <intent-filter> <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" /> </intent-filter> </receiver>
? ? 然后添加权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />?
下面就是代码层的实现:
在activity中创建一个创建快捷方式的方法:addShortCut();
public boolean addShortcut() { // 创建快捷方式的Intent Intent shortcutintent = new Intent( "com.android.launcher.action.INSTALL_SHORTCUT"); // 不允许重复创建 shortcutintent.putExtra("duplicate", false); // 快捷方式名称 shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname)); // 快捷方式图片 Parcelable icon = Intent.ShortcutIconResource.fromContext( getApplicationContext(), R.drawable.shortcut); shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); // 点击快捷图片,运行的程序主入口 shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent( getApplicationContext(), Activity01.class)); // 发送广播。OK sendBroadcast(shortcutintent); return true; }?