android 创建桌面快捷方式

1.在androidManifest文件中,想要创建快捷方式的Activity中加入android.intent.action.MAIN的action。缺少这行代码,点击快捷方式时,会提示没有安装这个应用。

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
</intent-filter>
Activity里面还要添加上android:launchMode="singleInstance"另起一个栈,否则点击这个activity快捷方式,会启动别的应用

另外,在Activity的配置文件中要加入android:icon,否则有些手机不会生成图标。

2.创建快捷方式的代码

public static void createLightShortCut(Context context) {
	Resources res = context.getResources();
	String name = res.getString(R.string.light_short_cut);
	if (hasShortcut(context, name)) {
		return;
	}
	Intent intent = new Intent();
	intent.setClass(context, AppLightActivity.class);
	intent.setAction("android.intent.action.MAIN");
	intent.addCategory("android.intent.category.LAUNCHER");
	try {
		Parcelable icon = Intent.ShortcutIconResource.fromContext(context.getApplicationContext(), R.drawable.light_app_icon);
		Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
		// 不允许重复创建
		shortcutintent.putExtra("duplicate", false);
		// 需要现实的名称
		shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
		// 快捷图片
		shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
		// 点击快捷图片,运行的程序主入口
		shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
		// 发送广播
		context.sendBroadcast(shortcutintent);
	} catch (Exception ex) {
		ex.printStackTrace();
	}
}
图标要用Parcelable不能用bitmap,否则图标会显示不全。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值