Android 12 创建桌面快捷方式:Java 代码示例
{"title":"Android 12 创建桌面快捷方式:Java 代码示例","description":"如何在 Android 12 上使用 Java 代码创建桌面快捷方式。提供详细代码示例和说明,以及对 Android 版本的限制。","keywords":"Android, 快捷方式, 桌面, Java, Android 12, 代码示例, 应用程序, 开发, 编程","content":"在 Android 12 中,可以使用以下代码在 Android 应用程序中创建快捷方式到桌面:\n\njava\nimport android.content.Intent;\nimport android.os.Build;\nimport android.support.annotation.RequiresApi;\n\n@RequiresApi(api = Build.VERSION_CODES.KITKAT)\npublic class ShortcutHelper {\n\n public static void createShortcutToDesktop(Context context, String shortcutName, int shortcutIconResource) {\n Intent shortcutIntent = new Intent(context, MainActivity.class); // 替换为你的MainActivity类\n shortcutIntent.setAction(Intent.ACTION_MAIN);\n\n Intent addIntent = new Intent();\n addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);\n addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);\n addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, shortcutIconResource));\n\n addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");\n context.sendBroadcast(addIntent);\n }\n}\n\n\n要创建快捷方式,请调用createShortcutToDesktop方法,并传入上下文(Context),快捷方式名称(shortcutName)和快捷方式图标资源(shortcutIconResource)。确保在你的MainActivity类中替换为你的主活动类。\n\n请注意,这段代码要求在Android 4.4及更高版本(包括Android 12)上运行。"}
原文地址: https://www.cveoy.top/t/topic/pKeO 著作权归作者所有。请勿转载和采集!