Android programming Add shortcut (short) to mobile desktop method (including Add, delete and query) _android

Source: Internet
Author: User

This article is an example of how Android programming adds shortcuts (short) to your mobile desktop. Share to everyone for your reference, specific as follows:

Permissions

To add a shortcut to your mobile desktop, you first need to add permissions to the manifest.

<!--add Shortcuts-->
<uses-permission android:name= "Com.android.launcher.permission.INSTALL_SHORTCUT"/ >
<!--removing shortcuts-->
<uses-permission android:name= "com.android.launcher.permission.UNINSTALL_ Shortcut "/>
<!--query Shortcuts-->
<uses-permission android:name=" Com.android.launcher.permission.READ_SETTINGS "/>

Add a shortcut

To add a shortcut, you send a broadcast of related action to your desktop application (launcher), and the relevant action is as follows:

Copy Code code as follows:
public static final String action_add_shortcut = "Com.android.launcher.action.INSTALL_SHORTCUT";

To add a shortcut:

 private void Addshortcut (String name) {Intent addshortcutintent = new Intent (action_a
  Dd_shortcut); Duplicate creation of Addshortcutintent.putextra ("duplicate", false) is not allowed;//The test is not based on the name of the shortcut to judge the repetition//should be based on the fast-chain of the intent to determine whether to repeat, That is, intent.extra_shortcut_intent field value//But the name is not at the same time, although some mobile phone systems will show toast prompts to repeat, will still build fast chain//screen no space will be prompted//note: Repeated creation of the behavior of MIUI and Samsung mobile phone
  Not quite the same, the millet seems unable to repeatedly create shortcuts//name Addshortcutintent.putextra (intent.extra_shortcut_name, name); Icon Addshortcutintent.putextra (Intent.extra_shortcut_icon_resource, Intent.ShortcutIconResource.fromContext (Mai
  Nactivity.this, R.drawable.ic_launcher));
  Set association program Intent launcherintent = new Intent (intent.action_main);
  Launcherintent.setclass (Mainactivity.this, Mainactivity.class);
  Launcherintent.addcategory (Intent.category_launcher);
  Addshortcutintent. Putextra (Intent.extra_shortcut_intent, launcherintent);
Send broadcast Sendbroadcast (addshortcutintent); }

Remove Shortcut

Remove action for shortcut:

Copy Code code as follows:
public static final String action_remove_shortcut = "Com.android.launcher.action.UNINSTALL_SHORTCUT";

Ways to remove Shortcuts:

The private void Removeshortcut (String name) {
  //Remove shortcut method does not work on the millet system and can be removed from Samsung on
  Intent Intent = new Intent ( Action_remove_shortcut);
  Name
  Intent.putextra (intent.extra_shortcut_name, name);
  Set the association program
  Intent launcherintent = new Intent (mainactivity.this,
      mainactivity.class). Setaction ( Intent.action_main);
  Intent.putextra (Intent.extra_shortcut_intent, launcherintent);
  Send broadcast
  sendbroadcast (intent);
}

Test on two mobile phones, found that the millet phone can not be removed after adding a shortcut, Samsung mobile phones.

Query shortcuts

The way to query whether the shortcut exists is from other information on the Internet, but failed to test the query, two mobile phone (millet, Samsung) are not found.

Just keep the code and see what the reason is:

Private Boolean hasinstallshortcut (String name) {
  Boolean hasinstall = false;
  Final String authority = "com.android.launcher2.settings";
  Uri Content_uri = Uri.parse ("content://" + Authority
      + "/favorites?notify=true");
  It
  's
  always failed to find provider info//Com.android.launcher2.settings and com.android.launcher.settings. Cursor Cursor = This.getcontentresolver (). Query (Content_uri,
      new string[] {"title", "Iconresource"}, "title=", C9/>new string[] {name}, null);
  if (cursor!= null && cursor.getcount () > 0) {
    Hasinstall = true;
  }
  return hasinstall;
}

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.