代码之家  ›  专栏  ›  技术社区  ›  Waza_Be

将他的应用程序添加到“添加到主屏幕”/“快捷方式”部分

  •  2
  • Waza_Be  · 技术社区  · 16 年前

    在我的Android应用程序中,我通过代码创建了一些快捷方式,用于我的应用程序中的某些活动。我使用广播来完成此功能:

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

    很酷,真的很管用!

    现在,我想做些不同的事情:我已经看到一些动作可以“注册”到Android菜单中的某个地方,当你长时间按这个键时:

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

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

    如何注册此菜单?我想在清单中有一些行需要添加,但看不到该在哪里添加!

    非常感谢您的帮助!

    顺便说一句,还有一个第二个问题:一旦我成功了,我可以在我的菜单中添加不同数量的快捷方式吗(假设我想为一个多帐户的Twitter客户端添加快捷方式,我想为列表中的每个Twitter帐户看到不同的快捷方式。)所以快捷方式的数量是以编程方式计算的。

    2 回复  |  直到 15 年前
        1
  •  8
  •   Waza_Be    16 年前

    刚刚在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>
    
        2
  •  1
  •   rogerkk    15 年前

    关于第二个问题:

    我不知道如何确切地实现您所要求的,但一个可能的解决方案可能是让您的CreateShortcuts活动(或等效活动)打开一个具有可能选项的对话框,从中您的活动将返回与所选项目对应的快捷方式。当然,还有一些附加信息(请参见intent.putextra())详细说明了在Twitter用户的情况下,用户选择了哪个项目。

    推荐文章