android开发时 桌面快捷方式的创建

发现每次第一次运行应用的时候有很多应用都会提示你桌面快捷方式已创建,所以今天就自己来实现下


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值