安卓桌面快捷方式

寫一個簡單的給安卓桌面增加快捷方式的例子,結果網上查詢到的代碼無論如何都無法實現。經過調查,發現是安卓從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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章