//给指定应用动态快捷方式添加指定Intent URI
importClass(android.content.Context);
importClass(android.content.pm.ShortcutInfo);
importClass(android.graphics.drawable.Icon);
importClass(android.content.Intent);
importClass(java.util.ArrayList);
importClass(java.util.Collections);
// ===== 目标包名 =====
var targetPackage = "tornaco.apps.shortx";

// ===== 指定要写入的 Intent URI =====
var intentUri = `intent:#Intent;action=com.tmessages.openchat0;component=org.telegram.messenger/.OpenChatReceiver;l.chatId=1604486631;end`;

try {
    var otherContext = context.createPackageContext(
        targetPackage,
        Context.CONTEXT_IGNORE_SECURITY
    );
    var shortcutManager =
        otherContext.getSystemService(Context.SHORTCUT_SERVICE);

    if (!shortcutManager) {
        throw "ShortcutManager 不可用";
    }
    var targetIntent = Intent.parseUri(intentUri, 0);
    targetIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
    // ===== 新快捷方式ID=====
    //ID用来区分不同的快捷方式,删除也必须使用同样ID
    var shortcutId = "shortcut_0001";

    var newShortcut =
        new ShortcutInfo.Builder(otherContext, shortcutId)
             //快捷方式 桌面显示名称
            .setShortLabel("ShortX群组")
            //长名称 
            .setLongLabel("ShortX群组")
            .setIcon(
                Icon.createWithResource(
                    otherContext,
                    otherContext.getApplicationInfo().icon
                )
            )
            .setIntent(targetIntent)
            .build();
    var existing = shortcutManager.getDynamicShortcuts();
    if (existing.size() >= shortcutManager.getMaxShortcutCountPerActivity()) {
        throw "动态快捷方式数量已达系统上限";
    }
    shortcutManager.addDynamicShortcuts(
        Collections.singletonList(newShortcut)
    );
      shortcutId;
} catch (e) {
    "写入失败: " + e;
}

#Javascript
 
 
Back to Top