Android Apps Create desktop shortcuts code _android

Source: Internet
Author: User

Android shortcuts are simple, you send a system broadcast, and then set intent for the shortcut---

Package com.xikang.android.slimcoach.utils;
/** * @author Huiych * Create shortcuts * @created 2013-02-21 * */import android.content.Intent;

Import android.os.Parcelable;
Import Com.xikang.android.slimcoach.AppXiKang;
Import COM.XIKANG.ANDROID.SLIMCOACH.R;

Import Com.xikang.android.slimcoach.ui.AppStart; public class Shortcututil {public static void Initshortcut () {Intent addshortcut = new Intent ("Com.android.launcher.act Ion.
 Install_shortcut ");
    You cannot create shortcuts repeatedly addshortcut.putextra ("duplicate", false); String title = Appxikang.getapp (). getString (R.string.app_name);//name parcelable icon = Intent.ShortcutIconResource.fro Mcontext (Appxikang.getapp (), R.drawable.icon)///icon//Click on the shortcut after the Operation Intent, after the shortcut is established, start the program again Intent Intent = new Intent  
    (Appxikang.getapp (), appstart.class);  
    Set the title of the shortcut Addshortcut.putextra (intent.extra_shortcut_name, title);  
    Set the icon for the shortcut Addshortcut.putextra (Intent.extra_shortcut_icon_resource, icon); Set the shortcut corresponding to the intent AddshorTcut.putextra (Intent.extra_shortcut_intent, Intent);
 Send broadcast Add shortcut Appxikang.getapp (). Sendbroadcast (Addshortcut);
 }
}

Appxikange.getapp () is the object that gets the activity.

Note that to set permissions in the manifest file

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

This way, when you want to add a shortcut, you can give the user a alertdialog, prompt, and then reference. It's OK.

There are also many applications in the marketplace where shortcuts are created directly when the application is installed. But such a realization is not very friendly. Not recommended for use.

The following is a complete code demonstration that uses a slightly different approach than the above:

public class Shortcututil {public static void Initshortcut (activity acti) {Intent shortcut = new Intent ("Com.android.lau 
Ncher.action.INSTALL_SHORTCUT "); 
The name of the shortcut is Shortcut.putextra (Intent.extra_shortcut_name, Appxikang.getapp (). getString (R.string.app_name)); Shortcut.putextra ("duplicate", false); Do not allow duplicate creation specifies the object that the current activity starts for the shortcut: such as//com.everest.video.videoplayer//NOTE: The second parameter of componentname must be preceded by a dot (.). Otherwise the shortcut cannot start the appropriate program ComponentName comp = new ComponentName (Appxikang.getapp (). Getpackagename (), "." 
+acti.getlocalclassname ()); 
Shortcut.putextra (Intent.extra_shortcut_intent, New Intent (Intent.action_main). SetComponent (comp)); The icon for the shortcut Shortcuticonresource iconres = Intent.ShortcutIconResource.fromContext (Appxikang.getapp (), 
R.drawable.icon); 
Shortcut.putextra (Intent.extra_shortcut_icon_resource, iconres);
Appxikang.getapp (). sendbroadcast (shortcut); The public static void Delshortcut (activity acti) {Intent shortcut = new Intent ("Com.android.launcher.action.UNINSTALL_S Hortcut "); 
        
    The name of the shortcut is Shortcut.putextra (Intent.extra_shortcut_name, Appxikang.getapp (). getString (R.string.app_name 
    )); 
    String Appclass = Appxikang.getapp (). Getpackagename () + "." +acti.getlocalclassname (); 
    ComponentName comp = new ComponentName (Appxikang.getapp (). Getpackagename (), appclass); 
    Shortcut.putextra (Intent.extra_shortcut_intent, New Intent (Intent.action_main). SetComponent (comp));
  Appxikang.getapp (). sendbroadcast (shortcut); }
}

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.