1. 程式人生 > >安卓桌面快捷方式

安卓桌面快捷方式

寫一個簡單的給安卓桌面增加快捷方式的例子,結果網上查詢到的程式碼無論如何都無法實現。經過調查,發現是安卓從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()); }

 


關於shortcut相關相信資訊 發現一篇已經寫的比較全的文章可以參考。 https://blog.csdn.net/mqcLuck/article/details/53586117