Aller au contenu

Tuto: Ajouter son appli dans "add to home screen"/"shortcuts"


Profete162

Recommended Posts

Salut à tous,

Ce post était à la base une question. Ayant trouvé ma réponse, je la transforme en tuto.

Le but est de rajouter un raccourci dans la section raccourci du launcher comme BBFOC ou ce screenshot:

UltimateFaves-Add-Home-Screen-Shortcut.jpg

Il faut donc rajouter quelques lignes dans le manifest et dire à son appli comment créer le raccourci

Lien vers le commentaire
Partager sur d’autres sites

Bon en fait, dans l'API Démo du SDK, ils mettent ce code dans le manifest:

                          android:label="@string/shortcuts">











           android:targetActivity=".app.LauncherShortcuts"
           android:label="@string/sample_shortcuts">







et l'activité est comme ceci:

>/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.profete162.EasyHome;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;


/**
* This Activity actually handles two stages of a launcher shortcut's 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.
*    
* We handle stage 1 (creating a shortcut) by simply sending back the information (in the form
* of an {@link android.content.Intent} that the launcher will use to create the shortcut.
* 
* You can also implement this in an interactive way, by having your activity actually present
* UI for the user to select the specific nature of the shortcut, such as a contact, picture, URL,
* media item, or action.
* 
* We handle stage 2 (responding to a shortcut) in this sample by simply displaying the contents
* of the incoming {@link android.content.Intent}.
* 
* In a real application, you would probably use the shortcut intent to display specific content
* or start a particular operation.
*/
public class LauncherShortcuts extends Activity {

   private static final String EXTRA_KEY = "com.example.android.apis.app.LauncherShortcuts";

   @Override
   public void onCreate(Bundle icicle) {
       super.onCreate(icicle);

       // Resolve the intent

       final Intent intent = getIntent();
       final String action = intent.getAction();

       // If the intent is a request to create a shortcut, we'll do that and exit

       if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
           setupShortcut();
           finish();
           return;
       }

       // If we weren't launched with a CREATE_SHORTCUT intent, simply put up an informative
       // display.

       // Inflate our UI from its XML layout description.

       setContentView(R.layout.launcher_shortcuts);

       // Provide a lightweight view of the Intent that launched us

       TextView intentInfo = (TextView) findViewById(R.id.txt_shortcut_intent);
       String info = intent.toString();
       String extra = intent.getStringExtra(EXTRA_KEY);
       if (extra != null) {
           info = info + " " + extra;
       }
       intentInfo.setText(info);
   }

   /**
    * This function creates a shortcut and returns it to the caller.  There are actually two 
    * intents that you will send back.
    * 
    * The first intent serves as a container for the shortcut and is returned to the launcher by 
    * setResult().  This intent must contain three fields:
    * 
    * </pre>
<ul>
    * {@link android.content.Intent#EXTRA_SHORTCUT_INTENT} The shortcut intent.
    * {@link android.content.Intent#EXTRA_SHORTCUT_NAME} The text that will be displayed with
    * the shortcut.
    * {@link android.content.Intent#EXTRA_SHORTCUT_ICON} The shortcut's icon, if provided as a
    * bitmap, or {@link android.content.Intent#EXTRA_SHORTCUT_ICON_RESOURCE} if provided as
    * a drawable resource.
    * </ul>
<br>    * <br>    * If you use a simple drawable resource, note that you must wrapper it using<br>    * {@link android.content.Intent.ShortcutIconResource}, as shown below.  This is required so<br>    * that the launcher can access resources that are stored in your application's .apk file.  If <br>    * you return a bitmap, such as a thumbnail, you can simply put the bitmap into the extras <br>    * bundle using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}.<br>    * <br>    * The shortcut intent can be any intent that you wish the launcher to send, when the user <br>    * clicks on the shortcut.  Typically this will be {@link android.content.Intent#ACTION_VIEW} <br>    * with an appropriate Uri for your content, but any Intent will work here as long as it <br>    * triggers the desired action within your Activity.<br>    */<br>   private void setupShortcut() {<br>       // First, set up the shortcut intent.  For this example, we simply create an intent that<br>       // will bring us directly back to this activity.  A more typical implementation would use a <br>       // data Uri in order to display a more specific result, or a custom action in order to <br>       // launch a specific operation.<br><br>       Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);<br>       shortcutIntent.setClassName(this, this.getClass().getName());<br>       shortcutIntent.putExtra(EXTRA_KEY, "ApiDemos Provided This Shortcut");<br><br>       // Then, set up the container intent (the response to the caller)<br><br>       Intent intent = new Intent();<br>       intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);<br>       intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "easyhome");<br>       Parcelable iconResource = Intent.ShortcutIconResource.fromContext(<br>               this,  R.drawable.icon);<br>       intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);<br><br>       // Now, return the result to the launcher<br><br>       setResult(RESULT_OK, intent);<br>   }<br>

Lien vers le commentaire
Partager sur d’autres sites

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

×
×
  • Créer...