创建android应用程序的桌面图标

我们安装一个应用程序后,应用程序会在手机的主页面上生成应用图标,这样用户如果要使用的话需要到主页面去找这个应用程序的图标,这样操作起来不太方便。所以个人感觉最好是在手机的桌面上创建一个应用程序的快捷方式,就像PC上的windows桌面快捷方式一样。这个在网上已经有实现的比较好的代码了,本文参考转载自:http://www.jb51.net/article/32457.htm

具体实现如下:


/**
* 创建桌面快捷方式
*/

static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
 private String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
 
    public void createShortCut(){
     SharedPreferences setting = getSharedPreferences("chentequan.preferences", 0);
     // 判断是否第一次启动应用程序(默认为true)
     boolean firstStart = setting.getBoolean("FIRST_START", true);

     if (!firstStart) {
      return;
     }
     Intent shortcutIntent = new Intent(ACTION_INSTALL_SHORTCUT);

       /* 设置快捷方式名称 */
     shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));

     /* 设置不允许重复创建 */
     shortcutIntent.putExtra(EXTRA_SHORTCUT_DUPLICATE, false);

     /* 需要使用ACTION_MAIN这个在卸载应用程序时,才会同步连桌面快捷方式也一起删除  */
     Intent intent2 = new Intent(Intent.ACTION_MAIN);
     intent2.addCategory(Intent.CATEGORY_LAUNCHER);

     /* 指定应用程序的首个启动页面*/    

     intent2.setComponent(new ComponentName(this.getPackageName(), ".loginactivity"));
     shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
     shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
     Intent.ShortcutIconResource.fromContext(this,
     R.drawable.sslvpnicon12_l));
     sendBroadcast(shortcutIntent);


     Editor editor = setting.edit();
     editor.putBoolean("FIRST_START", false);
     // 提交设置
     editor.commit();
    }

注意:这边要创建桌面快捷方式,需要申请用户权限,在androidMainfest.xml上添加如下代码即可:

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值