Android创建桌面快捷方式

本文介绍了一种在Android设备上创建桌面快捷方式的方法,解决了部分机型因包名冲突或设置问题导致快捷方式创建失败的问题。通过使用setAction创建Intent,并自定义action,可以成功在多数机型上创建桌面快捷方式。

在桌面上创建特定界面的快捷入口,icon和title根据请求参数命名。在网上收集的一些相关资 料,在使用intent发送广播的时候,一些型号的收集会有问题,如魅族MX,红米,以及华为,使用setClass和setComponent创建快捷 方式的时候不能正确生成快捷方式,有的快捷方式的会使用相应包下的APP名称命名,有的手机如魅族会应为包名冲突而无法创建快捷方式。解决的办法就是使用 setAction来创建Intent,然后可以在data/data/com.andoird.launcher/databases /launcher.db中查看桌面快捷方式的数据是否正确。以下是相关代码:

  Intent intent = new Intent("cn.example.action.SHORTCUT");
//自定义action
  
  intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  
  Bundle bundle = new Bundle();
  bundle.putString("type", "default");
  intent.putExtras(bundle);
  
  //创建快捷方式的Intent
  Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
  //不允许重复创建
  shortcutintent.putExtra("duplicate", false);
  //需要现实的名称
  shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式");
  //快捷图片
  Parcelable icon = Intent.ShortcutIconResource.fromContext(activity.getApplicationContext() , R.drawable.logo);
  shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
  //点击快捷图片,运行的程序主入口
  shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
  //发送广播。OK
  activity.sendBroadcast(shortcutintent);

然后在快捷启动对应的activity下加上intent-filter:
<intent-filter>
<action android:name="cn.kuwo.player.action.SHORTCUT" />
</intent-filter>

这样就可以适应大多数机型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值