发现每次第一次运行应用的时候有很多应用都会提示你桌面快捷方式已创建,所以今天就自己来实现下
public static void createShortCut(Activity act, int iconResId, int appnameResId) {
Intent intent = new Intent();
intent.setClass(act, WelcomeActivity.class);
//让桌面快捷方式和我的应用绑定
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
Intent i = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
i.putExtra("duplicate", false);// 不允许重复创建
i.putExtra(Intent.EXTRA_SHORTCUT_NAME, act.getString(appnameResId));// 需要现实的名称
Parcelable icon = Intent.ShortcutIconResource.fromContext(act.getApplicationContext(),
iconResId);// 快捷图片
i.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); // 点击快捷图片,运行的程序主入口
// 发送广播
act.sendBroadcast(i);
}
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
还有,有的人会说,你们自己写的可能删除应用时,桌面快捷方式依旧存在,不要担心,按照我上面写的就好啦
<span style="color:#ff6666;"><span style="font-size:24px;">intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER"</span>);</span>
这两句就是把你的应用和桌面快捷方式绑定到一起
还有的同学们要说了,每次运行的时候,都会提示,好心烦,因为在网上找的一些判断桌面快捷方式是否存在的代码并不起作用,可能是因为很多公司都改了launcher等,
我的做法是放到准备运行引导界面的那里,这样就只有第一次才会创建啦
在这里把我在网上找的一些优秀博客贴上来,大家都是不容易哒
http://orgcent.com/android-add-del-shortcut-desktop/
http://www.2cto.com/kf/201110/107612.html