Android app Add shortcut

Source: Internet
Author: User

Implementation principle: The use of intent, the code is as follows:
1 //Create a shortcut2     Private voidCreateShortcut () {3Intent shortcut =NewIntent ("Com.android.launcher.action.INSTALL_SHORTCUT"); 4 Shortcut.putextra (Intent.extra_shortcut_name, getString (r.string.app_name)); 5Intent Intent =NewIntent (Intent.action_main); 6 intent.addcategory (Intent.category_launcher); 7Intent.setclass ( This, Mainactivity.class);//set the first page8 Shortcut.putextra (intent.extra_shortcut_intent, Intent); 9Shortcuticonresource iconres = Intent.ShortcutIconResource.fromContext ( This, R.drawable.ic_launcher); Ten Shortcut.putextra (Intent.extra_shortcut_icon_resource, iconres);  One sendbroadcast (shortcut);  AToast.maketext (Getapplicationcontext (), "created successfully", Toast.length_short). Show (); -     }   -     //To interpret if a shortcut already exists the      Public Booleanisexistshortcut () { -         BooleanIsinstallshortcut =false;  -         FinalContentresolver cr = Mainactivity. This. Getcontentresolver ();  -         FinalString authority = "Com.android.launcher2.settings";  +         FinalUri Content_uri = Uri.parse ("content://" + Authority + "/favorites?notify=true");  -Cursor C = cr.query (Content_uri,NewString[] {"title", "Iconresource"}, "Title=?",NewString[] {getString (r.string.app_name)},NULL);  +         if(c! =NULL&& c.getcount () > 0) {   AIsinstallshortcut =true;  at         }   -         returnIsinstallshortcut;  -}
And then use the time:
1 if (Isexistshortcut ()) {2     Toast.maketext (Getapplicationcontext (), "Shortcut already exists", Toast.length_short). Show (); 3 }else{4    createshortcut (); 5 }

Remember to add permissions in the manifest file:

1     <uses-permission android:name= "Com.android.launcher.permission.INSTALL_SHORTCUT"/>2     < Uses-permission android:name= "Com.android.launcher.permission.READ_SETTINGS"/>3  < Uses-permission android:name= "Com.android.launcher.permission.WRITE_SETTINGS"/>

Android app Add shortcut

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.