Android 为应用程序创建桌面快捷方式

我们开发一款软件后,如果手机装的软件过多,去翻的话会很难翻的,所以,在主页面有一个快捷方式的话会很不错的,下面是详细代码:

/** 
* 创建桌面快捷方式 
*/ 
private void createShortcut() { 
SharedPreferences setting = getSharedPreferences("silent.preferences", 0); 
// 判断是否第一次启动应用程序(默认为true) 
boolean firstStart = setting.getBoolean("FIRST_START", true); 
// 第一次启动时创建桌面快捷方式 
if (firstStart) { 
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 
// 快捷方式的名称 
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name2)); 
// 不允许重复创建 
shortcut.putExtra("duplicate", false); 
// 指定快捷方式的启动对象 
ComponentName comp = new ComponentName(this.getPackageName(), "." + this.getLocalClassName()); 
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp)); 
// 快捷方式的图标 
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.zhangxy);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); 
// 发出广播 
sendBroadcast(shortcut); 
// 将第一次启动的标识设置为false 
Editor editor = setting.edit(); 
editor.putBoolean("FIRST_START", false); 
// 提交设置 
editor.commit(); 
} 
} 

然后在onCreate()方法里加上上面方法名称就行了: 

// 安装后第一次启动时创建桌面快捷方式 
createShortcut(); 
最后在AndroidManifest.xml里加上创建快捷方式的权限就行了: 

<!-- 创建桌面快捷方式的权限 --> 
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 

来源:http://www.jb51.net/article/32457.htm

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值