2

在我的 android 应用程序中,我通过代码为我的应用程序中的某些活动创建快捷方式。我通过使用广播来执行此功能:

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

这很酷,真的很管用!

现在我想做一些不同的事情:当你长按 Home 时,我已经看到一些操作可以“注册”到某个地方添加到 Android 菜单中,如下所示:

http://www.androidtapp.com/wp-content/uploads/2010/02/UltimateFaves-Add-Home-Screen-Shortcut.jpg

所以我的主要问题是下一个:

怎么可能注册这个菜单。我想清单中有一些要添加的行,但看不到在哪里执行此操作!

非常感谢您的帮助!

顺便说一句,还有一个次要问题:一旦我成功做到这一点,我可以在我的菜单中添加不同数量的快捷方式吗(想象一下我想为多帐户 twitter 客户端这样做,我想看到一个此列表中的每个 Twitter 帐户都不同。)因此,快捷方式的数量是通过编程计算的。

4

2 回答 2

8

刚刚在 SDK 示例中找到了我的答案:

    <!-- This section of sample code shows how your application can add shortcuts to -->
    <!-- the launcher (home screen).  Shortcuts have a three step life cycle. -->

    <!-- 1.  Your application offers to provide shortcuts to the launcher.  When -->
    <!--     the user installs a shortcut, an activity within your application -->
    <!--     generates the actual shortcut and returns it to the launcher, where it -->
    <!--     is shown to the user as an icon. -->

    <!-- 2.  Any time the user clicks on an installed shortcut, an intent is sent. -->
    <!--     Typically this would then be handled as necessary by an activity within -->
    <!--     your application. -->

    <!-- 3.  The shortcut is deleted.  There is no notification to your application. -->

    <!-- In order provide shortcuts from your application, you provide three things: -->

    <!-- 1.  An intent-filter declaring your ability to provide shortcuts -->
    <!-- 2.  Code within the activity to provide the shortcuts as requested -->
    <!-- 3.  Code elsewhere within your activity, if appropriate, to receive -->
    <!--     intents from the shortcut itself. -->

    <activity android:name=".LauncherShortcuts"
              android:label="launchers">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.SAMPLE_CODE" />
        </intent-filter>

    </activity>

    <!-- It is recommended that you use an activity-alias to provide the "CREATE_SHORTCUT" -->
    <!-- intent-filter.  This gives you a way to set the text (and optionally the -->
    <!-- icon) that will be seen in the launcher's create-shortcut user interface. -->

    <activity-alias android:name=".CreateShortcuts"
        android:targetActivity=".LauncherShortcuts"
        android:label="test">

        <!--  This intent-filter allows your shortcuts to be created in the launcher. -->
        <intent-filter>
            <action android:name="android.intent.action.CREATE_SHORTCUT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
于 2010-07-22T12:05:48.777 回答
1

至于你的第二个问题:

我不知道如何准确地实现您的要求,但一种可能的解决方案可能是让您的 CreateShortcuts 活动(或等效活动)打开一个包含可能选择的对话框,您的活动将返回对应于选定的项目。当然还有一些额外的信息(参见 Intent.putExtra()),详细说明了用户选择的项目,在你的情况下是 twitter 用户。

于 2011-01-17T07:42:08.207 回答