寫一個簡單的給安卓桌面增加快捷方式的例子,結果網上查詢到的代碼無論如何都無法實現。經過調查,發現是安卓從7.1開始引入shotcut
如果你用的androidsdk25那麼要使用下面的方式創建快捷方式
//androidsdk25開始 也就是安卓7開始 採用統一的方式管理桌面快捷方式 ShortcutManager shortcutManager = (ShortcutManager) getApplicationContext().getSystemService( Context.SHORTCUT_SERVICE); if (shortcutManager.isRequestPinShortcutSupported()) { Intent shortcutInfoIntent = new Intent(getApplicationContext(), MainActivity.class); shortcutInfoIntent.setAction(Intent.ACTION_VIEW); //這裏要注意的是android:action一定要配置, 否則會崩潰 ShortcutInfo info = new ShortcutInfo.Builder(getApplicationContext(), "The only id") .setIcon( Icon.createWithResource(getApplicationContext(), R.drawable.heart)) .setShortLabel("Short Label") .setIntent(shortcutInfoIntent) .build(); //當添加快捷方式的確認彈框彈出來時,將被回調 PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(getApplicationContext(), PermissionActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender()); }