Android 创建快捷方式失效的bug【已解决】要特别注意以下这句话导致的无法创建快捷方式的问题intent.putExtr
Android 创建快捷方式失效的bug【已解决】
要特别注意以下这句话导致的无法创建快捷方式的问题
intent.putExtra("duplicate", false);很多地方都说这句话的意义是在于防止重复创建快捷方式,但是我今天遇到的问题就是由于这句话导致无法在桌面上创建快捷方式。
最后我的做法是:
1.用SharedPreferences来判断是否是第一次进入应用
2.用isInstallShortcut()来判断是否存有快捷方式
3.创建快捷方式(没有duplicate属性)
1.
/** * 创建快捷方式 * */ public void createShortCut(Context contxt) { if (isInstallShortcut()) {// 如果已经创建了一次就不会再创建了 return; } Intent sIntent = new Intent(Intent.ACTION_MAIN); sIntent.addCategory(Intent.CATEGORY_LAUNCHER);// 加入action,和category之后,程序卸载的时候才会主动将该快捷方式也卸载 sIntent.setClass(contxt, Login.class); Intent installer = new Intent();// installer.putExtra("duplicate", false); installer.putExtra("android.intent.extra.shortcut.INTENT", sIntent); installer.putExtra("android.intent.extra.shortcut.NAME", "XXXX"); installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(contxt, R.drawable.icon)); installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); contxt.sendBroadcast(installer); }测试:点击
