Adding his application to the “add to Home screen”

2019-05-28 11:07发布

问题:

In my android application, I create shortcuts by code to some activities within my application. I do this feature by using the broadcast:

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

and that's cool, that really work!

Now I would like to do something different: I have seen that some actions can "register" somewhere to be added in the Android menu, when you long press on the Home like this one:

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

So my main question is the next one:

How is that possible to register for this menu. I guess there is some line to add in the manifest, but cannot see where to do that!

Thank a lot for any help!

BTW, there is a secondary question: once i will have succeed doing that, may I add a different number of shortcuts in my menu ( imagine That I would like to do that for a multi-account twitter client, I would like to see a different for each twitter account in this list.) So the number of shortcut is programmaticaly computed.

回答1:

Just found my answer in the SDK samples:

    <!-- 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:

As for your secondary question:

I don't know how to achieve exactly what you are asking for, but one possible solution might be to have your CreateShortcuts activity (or equivalent) open up a dialog with the possible choices, from which your activity would return a shortcut corresponding to the selected item. Of course with some additional info (see Intent.putExtra()) detailing which item, in your case twitter user, the user selected.