主屏幕快捷方式活动 [英] Home screen shortcut to activity

查看:188
本文介绍了主屏幕快捷方式活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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>

要我的第二个活动,它只是我多么希望它做的事,但现在我有我的应用程序2启动图标。

To my second activity, it works just how I wish it to do, but now I have 2 launcher icons in my application.

我想的是,我心底有一个启动应用程序和主屏幕快捷方式到我的第二个活动。

What I wish is that I`ll have one launcher application and home screen shortcut to my second activity.

推荐答案

这也正是它的工作原理。您不能添加快捷方式actvity无法启动,因为它只是使没有真正意义。并允许用户启动手工的活动,说意图过滤器是强制性的。

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.

这篇关于主屏幕快捷方式活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆