Add a shortcut to your app

Source: Internet
Author: User

Ext.: http://blog.csdn.net/yangdeli888/article/details/8984018

I believe that everyone in the use of many applications have encountered such a situation, that is, when we first click on the use of this application program will be active for us to create a desktop shortcut, the advantage is to allow users more convenient access to the application, save the trouble of finding. There is a saying once that who occupies the PC desktop client who is the king. So we will certainly be useful in the development of this point, so today I will share with you the implementation of such a function. It's not hard, you just need to add one of these methods to the first activity that your application launches:

Method/Step
  1. Add such a method to the activity:

    /**

    * Create Shortcut

    */

    public void Createdeskshortcut () {

    LOG.I ("coder", "------createshortcut--------");

    Intent to create a shortcut

    Intent shortcutintent = new Intent (

    "Com.android.launcher.action.INSTALL_SHORTCUT");

    Do not allow duplicate creation

    Shortcutintent.putextra ("duplicate", false);

    Need a real name

    Shortcutintent.putextra (Intent.extra_shortcut_name,

    GetString (R.string.app_name));

    Quick picture

    parcelable icon = Intent.ShortcutIconResource.fromContext (

    Getapplicationcontext (), r.drawable.ic_launcher);

    Shortcutintent.putextra (Intent.extra_shortcut_icon_resource, ICON);

    Intent Intent = new Intent (Getapplicationcontext (),

    Androidlayoutactivity.class);

    Click on the shortcut picture, run the program main entrance

    Shortcutintent.putextra (Intent.extra_shortcut_intent, Intent);

    Send a broadcast. Ok

    Sendbroadcast (shortcutintent);

    }

  2. If you just add this code, when you uninstall your application you will find that there is a problem that your application is uninstalled, but the shortcut on the desktop is not uninstalled. Oh, in fact, you just need to set the corresponding boot into the intent plus so the following two properties is to show that your app is bound.

    The following two properties are intended to remove a shortcut on the desktop when the application is uninstalled

    Intent.setaction ("Android.intent.action.MAIN");

    Intent.addcategory ("Android.intent.category.LAUNCHER");

  3. Give the full code:

    Package com.jiahui.layout;

    Import android.app.Activity;

    Import Android.content.Context;

    Import android.content.Intent;

    Import android.content.SharedPreferences;

    Import Android.os.Bundle;

    Import android.os.Parcelable;

    Import Android.util.Log;

    public class Androidlayoutactivity Extendsactivity {

    /**called when the activity is first created. */

    @Override

    Publicvoid onCreate (Bundle savedinstancestate) {

    Super.oncreate (savedinstancestate);

    Setcontentview (R.layout.view_personal_info);

    Sharedpreferencespreferences = Getsharedpreferences ("First",

    Context.mode_private);

    Booleanisfirst = Preferences.getboolean ("Isfrist", true);

    if (IsFirst) {

    Createdeskshortcut ();

    }

    Sharedpreferences.editoreditor = Preferences.edit ();

    Editor.putboolean ("Isfrist", false);

    Editor.commit ();

    }

    /**

    * Create Shortcut

    */

    Publicvoid Createdeskshortcut () {

    LOG.I ("coder", "------createshortcut--------");

    Intent to create a shortcut

    Intentshortcutintent = new Intent (

    "Com.android.launcher.action.INSTALL_SHORTCUT");

    Do not allow duplicate creation

    Shortcutintent.putextra ("duplicate", false);

    Need a real name

    Shortcutintent.putextra (Intent.extra_shortcut_name,

    GetString (R.string.app_name));

    Quick picture

    Parcelableicon = Intent.ShortcutIconResource.fromContext (

    Getapplicationcontext (), r.drawable.ic_launcher);

    Shortcutintent.putextra (Intent.extra_shortcut_icon_resource,icon);

    Intentintent = new Intent (Getapplicationcontext (),

    Androidlayoutactivity.class);

    The following two properties are intended to remove a shortcut on the desktop when the application is uninstalled

    Intent.setaction ("Android.intent.action.MAIN");

    Intent.addcategory ("Android.intent.category.LAUNCHER");

    Click on the shortcut picture, run the program main entrance

    Shortcutintent.putextra (intent.extra_shortcut_intent,intent);

    Send a broadcast. Ok

    Sendbroadcast (shortcutintent);

    }

    }

  4. Don't forget to add the following permissions to the Androidmanifest.xml:

    <uses-permission android:name= "Com.android.launcher.permission.INSTALL_SHORTCUT"/>

Add a shortcut to your app

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.