我有 2 项活动:
1.主要活动
2.第二个活动
我希望创建第二个活动的主屏幕快捷方式:
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>
对于我的第二个活动,它按照我希望的方式工作,但现在我的应用程序中有 2 个启动器图标。
我希望我的第二个活动有一个启动器应用程序和主屏幕快捷方式。
这正是它的工作原理。您无法为无法启动的活动添加快捷方式,因为它根本没有任何实际意义。并允许用户手动启动该活动,说intent-filter
是强制性的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)