gpt4 book ai didi

android - Activity 的主屏幕快捷方式

转载 作者:行者123 更新时间:2023-11-29 01:46:57 25 4
gpt4 key购买 nike

我有 2 个 Activity :

1.MainActivity

2.SecondActivity

我想创建第二个 Activity 的主屏幕快捷方式:

Intent shortcutIntent = new Intent(getApplicationContext(), SecondActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);

Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"HelloWorldShortcut");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.ic_launcher));

addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);

当我单击快捷方式时,它什么也没做,我从启动器中得到提示“无法访问应用程序”。但是当我添加:

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

对于我的第二个 Activity ,它按照我希望的方式工作,但现在我的应用程序中有 2 个启动器图标。

我希望我的第二个 Activity 有一个启动器应用程序和主屏幕快捷方式。

最佳答案

这正是它的工作原理。您不能向无法启动的 Activity 添加快捷方式,因为它根本没有任何意义。为了允许用户手动启动该 Activity ,intent-filter 是必需的。

关于android - Activity 的主屏幕快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20901822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com