Android programming approach to adding shortcuts to the desktop _android

Source: Internet
Author: User

This article describes the Android programming implementation approach to adding shortcuts to the desktop. Share to everyone for your reference, specific as follows:

Sometimes you need to add shortcuts to your desktop for ease of use, and here are two ways to add shortcuts:

Method 1:

void Setshortcut () {
  Intent addshortcut = new Intent ();
  Set the name of the shortcut
  Addshortcut.putextra (intent.extra_shortcut_name, "shortcut Practice");
  Build a special icon in a shortcut
  parcelable icon = Intent.ShortcutIconResource.fromContext (
    shortcuttest.this, R.drawable.icon);
  Add shortcut icon
  Addshortcut.putextra (Intent.extra_shortcut_icon_resource, icon);
  Build shortcut Execution Intent
  Intent mailto = new Intent (this, shortcuttest.class);
  Add Shortcut Intent
  addshortcut.putextra (intent.extra_shortcut_intent, mailto);
  Normal
  Setresult (RESULT_OK, addshortcut);
}

Through the above code to add shortcuts, can only be added in the program startup, can not be added to the event, and now do not want to understand what is not, and did not study carefully, want to know the friend notice! And when you add this, you don't add it directly to your desktop, but you add it to the Add Shortcut window, as shown in the following illustration:

Method 2:

private void Addshortcut () {
  Intent shortcut = new Intent ("Com.android.launcher.action.INSTALL_SHORTCUT");
  The name of the shortcut
  Shortcut.putextra (intent.extra_shortcut_name, getString (R.string.app_name));  Shortcut.putextra ("duplicate", false);//Do not allow duplicate creation
  //Specify the object that the current activity starts for the shortcut: Com.everest.video.VideoPlayer
  //NOTE: The second parameter of componentname must be preceded by a dot (.), otherwise the shortcut cannot start the program
  //componentname comp = new ComponentName (This.getpackagename (), "." +this.getlocalclassname ());
  Shortcut.putextra (Intent.extra_shortcut_intent, New Intent (Intent.action_main). SetComponent (comp));
  Shortcut.putextra (Intent.extra_shortcut_intent, New Intent (This,shortcuttest.class));
  Shortcut icon
  shortcuticonresource iconres = Intent.ShortcutIconResource.fromContext (this, r.drawable.icon);
  Shortcut.putextra (Intent.extra_shortcut_icon_resource, iconres);
  Sendbroadcast (shortcut);
}

which

Shortcut.putextra ("duplicate", false);

Indicates that no duplicate settings are allowed

This code, added directly to the desktop, is also shown in the image above, but you need to add the following permissions:

Copy Code code as follows:
<uses-permission android:name= "Com.android.launcher.permission.INSTALL_SHORTCUT"/>

PS: More about Android manifest rights control can refer to the site online tools:

Android manifest Features and Permissions description Encyclopedia:
Http://tools.jb51.net/table/AndroidManifest

For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android debugging techniques and common problems solution summary", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", " Android Basic Components Usage Summary, Android View tips Summary, Android layout layout tips and Android Control usage summary

I hope this article will help you with the Android program.

Related Article

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.