应用创建快捷图标有两种方式,
一种是直接代码实现创建快捷图标(桌面直接生成);一种是长按桌面,在弹出的快捷菜单中生成
这里介绍代码实现 创建快捷图标 两个步骤
(1) 在AndroidManifest.xml 申请权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
(2) 启动Activity 中
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
createShortCut();
Log.d(tag, "ActivityA onCreate " + this);
}
public void createShortCut()
{
// 创建快捷方式的Intent
Intent shortcutintent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 不允许重复创建
shortcutintent.putExtra("duplicate", false);
// 需要现实的名称
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
// 快捷图片
Parcelable icon = Intent.ShortcutIconResource.fromContext(
getApplicationContext(), R.drawable.ic_launcher);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
Intent mainIntent = new Intent(Intent.ACTION_MAIN);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mainIntent.setClass(this, this.getClass());
// 点击快捷图片,运行的程序主入口
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mainIntent);
// 发送广播。OK
sendBroadcast(shortcutintent);
}
注意如上:mainIntent 设置Intent.ACTION_MAIN 和 addCategory(Intent.CATEGORY_LAUNCHER)不能少,
否则 每次点击 生成的快捷 图标 ,假如已经打开了应用 ,则会多次打开启动Activity
参考文章:
http://blog.youkuaiyun.com/jdsjlzx/article/details/21631747
http://blog.youkuaiyun.com/maylian7700/article/details/6801451