Android How to create a desktop shortcut _android

Source: Internet
Author: User

Android shortcuts for creating desktops
Overview: Creating a desktop shortcut is equivalent to creating an entry for a program, just as our program automatically creates an icon to the desktop after the installation is complete. In fact, create a desktop shortcut is similar to creating a program entrance, but like QQ session to create a QQ friend session shortcut, you have to create a dynamic icon, name.

1. First, authority is essential.

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

2. Then it is in your project configuration file configuration

 <activity
      android:name= "com.easemob.chatuidemo.activity.ChatActivity" >
      <intent-filter>
        <category android:name= "Android.intent.category.LAUNCHER"/>
        <action android:name= " Android.intent.action.CREATE_SHORTCUT "/>
      </intent-filter>
  </activity>

This actvity is the activity that you want to jump after clicking the shortcut

3. Then is the way you want to create shortcuts.

The code is as follows:

 public void Createshotcut (final context, final class<?> clazz, final string name, final string image
    ) {Intent shortcutintent = new Intent (intent.action_main);
    After adding the action, and CATEGORY, the program will not take the initiative to uninstall the shortcut also Shortcutintent.addcategory (intent.category_launcher);
    Shortcutintent.setclass (context, clazz);
    /** * Create a Bundle object to save the value that will be passed/Bundle Bundle = new Bundle ();
    Bundle.putstring ("UserId", userId);
    Shortcutintent.putextras (bundle); /** * Setting this property allows you to turn off other activity on the other task stack after clicking the shortcut, and then create the specified acticity/shortcutintent.setflags (Intent.flag_activity_c

    Lear_top);
    Create a shortcut to the Intent Intent shortcut = new Intent (intent.action_create_shortcut);
    Duplicate creation of Shortcut.putextra ("duplicate", false) is not allowed;
    Click on the quick picture, run the program main entrance Shortcut.putextra (intent.extra_shortcut_intent, shortcutintent);

    Need realistic name Shortcut.putextra (intent.extra_shortcut_name, name); Quick picture parcelable icon = intent.shortcuticonresOurce.fromcontext (Getapplicationcontext (), r.drawable.ic_launcher);
    Shortcut.putextra (Intent.extra_shortcut_icon_resource, ICON);
    Shortcut.setaction ("Com.android.launcher.action.INSTALL_SHORTCUT");
  Context.sendbroadcast (shortcut);

 }

The importance of this line of code is in the absence of this line, when you click on this shortcut, the jump will jump directly to the top of the application's stack (if the specified activity is on top of the stack and will not jump on it instead of being destroyed) instead of the specified activity (when you first didn't add this attribute, Never jumps to the specified activity).

Shortcutintent.setflags (Intent.flag_activity_clear_top);

If you want to add a picture dynamically to create a shortcut when you get the picture on the network to set its shortcut picture to use

Intent.extra_shortcut_icon is a bitmap object.
Shortcut.putextra (INTENT.EXTRA_SHORTCUT_ICON,BITMAP);

This line of code, you can request a network image after converting to bitmap after setting in.

OK dynamic Create shortcut that's done.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.