Android创建桌面快捷方式

在桌面上创建特定界面的快捷入口,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>

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

原文地址:https://www.cnblogs.com/Free-Thinker/p/3529183.html