简体   繁体   English

主屏幕活动的快捷方式

[英]Home screen shortcut to activity

I have 2 Activities: 我有2个活动:

1.MainActicity 1.MainActicity

2.SecondActivity 2.SecondActivity

I wish to create home screen shortcut to second 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); 

When I click the shortcut it does nothing and I get toast from my launcher "Unable access the application". 当我单击快捷方式时,它什么也没有做,并且从启动器“无法访问该应用程序”中敬酒。 But when I add: 但是当我添加:

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

To my second activity, it works just how I wish it to do, but now I have 2 launcher icons in my application. 在我的第二个活动中,它按我希望的方式工作,但是现在我的应用程序中有2个启动器图标。

What I wish is that I`ll have one launcher application and home screen shortcut to my second activity. 我希望我将拥有一个启动器应用程序和第二个活动的主屏幕快捷方式。

This is exactly how it works. 这就是它的工作原理。 You cannot add shortcut to actvity that cannot be launched because it simply makes no real sense. 您无法为无法启动的活动添加快捷方式,因为这根本没有任何意义。 And to allow user to start that activity by hand, said intent-filter is mandatory. 为了允许用户手动启动该活动,该intent-filter是强制性的。

Try removing LAUNCHER as a category (replace with DEFAULT ). 尝试删除LAUNCHER作为类别(用DEFAULT代替)。 That's what's sticking it in the app drawer. 这就是将其粘贴在应用程序抽屉中的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM