[Android Pro] Create a shortcut, delete a shortcut, query whether a shortcut exists

Source: Internet
Author: User

1: Create a shortcut

Permissions Required: <uses-permission android:name= "com.android.launcher.permission.INSTALL_SHORTCUT"/> 

Private Static voidCreateShortcut (Context cxt, String shortcutname,intshortcuticonres, String className,BooleanDuplicateBooleanlauchercategory) {Intent Intent=getshortcutintent (CXT, Cxt.getpackagename (), ClassName, Shortcutname, lauchercategory); intIconsize =cxt.getresources (). Getdimensionpixelsize (Res.dimen.app_icon_size); bitmapdrawable icon=(bitmapdrawable) cxt.getresources (). getdrawable (Shortcuticonres); Bitmap BMP= Imageutils.scaleto (Icon.getbitmap (), Iconsize, Iconsize,false);        Intent.putextra (Intent.extra_shortcut_icon, BMP);        Intent.putextra (Extra_shortcut_duplicate, DUPLICATE); //Now , notify the Launcher to create the shortcutCxt.sendbroadcast (Intent); }
Private StaticIntent getshortcutintent (Context cxt, String pkgname, String className, String shortcutname,Booleanlauchercategory) {        //Prepare The intents for shortcutIntent shortcutintent =NewIntent (Intent.action_view);        Shortcutintent.setclassname (Pkgname, className);        Shortcutintent.setflags (Intent.flag_activity_new_task);        Shortcutintent.putextra (Constants.extra_from_key, constants.extra_from_value_shortcut); if(lauchercategory) {shortcutintent.addcategory (intent.category_launcher);        Shortcutintent.setaction (Intent.action_main); } Intent Intent=NewIntent (action_install_shortcut);        Intent.putextra (Intent.extra_shortcut_intent, shortcutintent);        Intent.putextra (Intent.extra_shortcut_name, shortcutname); returnIntent; }

2: Delete shortcut (MIUI system not supported):

Permissions Required: <uses-permission android:name= "com.android.launcher.permission.UNINSTALL_SHORTCUT"/>

 Public Static voidremoveshortcut (Context cxt, String shortcutname, String className,BooleanRemoveAll) {        //Prepare The intents for shortcutIntent shortcutintent =NewIntent (Intent.action_view);        Shortcutintent.setclassname (CXT, className); Intent Intent=NewIntent (action_uninstall_shortcut);        Intent.putextra (Intent.extra_shortcut_intent, shortcutintent);        Intent.putextra (Intent.extra_shortcut_name, shortcutname);        Intent.putextra (Extra_shortcut_duplicate, RemoveAll); //Now , notify the Launcher to remove the shortcutCxt.sendbroadcast (Intent); }

3: Query shortcut exists (three-party ROM most queries fail with cursor NULL)

Permissions Required: <uses-permission android:name= " Com.android.launcher.permission.READ_SETTINGS "/>

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

Private Booleanhasshortcut () {BooleanIsinstallshortcut =false; FinalContentresolver CR =Activity.getcontentresolver (); FinalString authority = "Com.android.launcher.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). Trim ()},NULL); if(c!=NULL&& C.getcount () >0){
String title = c.getString(c.getColumnIndexOrThrow( "title" )); isinstallshortcut
=true ; } returnIsinstallshortcut; }

[Android Pro] Create a shortcut, delete a shortcut, query whether a shortcut exists

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.