Create and detect shortcuts in Android

Source: Internet
Author: User

Public class shortcututil {</P> <p>/** <br/> * Create a shortcut <br/> * @ Param context <br/> * /<br/> Public static void createshortcut (context, class <?> Target) {<br/> // <uses-Permission Android: Name = "com. android. launcher. permission. install_shortcut "/> <br/> intent = new intent (" com. android. launcher. action. install_shortcut "); <br/> intent. putextra (intent. extra_shortcut_icon_resource, intent. using cuticonresource. fromcontext (context, R. drawable. icon); <br/> intent. putextra (intent. extra_shortcut_name, context. getstring (R. string. app_name); <br /> Intent. putextra ("DUPLICATE", false); <br/> intent sintent = new intent (intent. action_main); <br/> sintent. addcategory (intent. category_launcher); // after adding action and category, <SPAN class = 'wp _ keywordlink '> the shortcut is automatically uninstalled only when the program is uninstalled </span> <br/> sintent. setclass (context, target); <br/> intent. putextra (intent. extra_shortcut_intent, sintent); <br/> context. sendbroadcast (intent); <br/>}</P> <p>/** <br/> * determines whether a shortcut exists on the desktop. <Br/> * @ return <br/> */<br/> Public static Boolean hasshortcut (context) {<br/> final contentresolver Cr = context. getcontentresolver (); <br/> final string authority = "com. android. launcher. settings "; <br/> final URI content_uri = Uri. parse ("content: //" + authority + "/favorites? Policy = true "); <br/> // <uses-Permission Android: Name =" com. android. launcher. permission. read_settings "/> <br/> cursor c = CR. query (content_uri, new string [] {"intent"}, null); <br/> If (C = NULL) {<br/> // note: In versions 2.1update and 2.2, the real machine cannot access COM. android. launcher. settings, 2.1update1 simulators can be <br/> // error/activitythread (1136): failed to find provider info for com. android. launcher. settings <br/> return false; <br/>}< br/> while (C. movetonext () {<br/> string intentstring = C. getstring (C. getcolumnindex ("intent"); <br/> If (intentstring = NULL) {<br/> continue; <br/>}< br/> string componentstring = getcomponentstring (intentstring); <br/> If (componentstring. startswith (context. getpackagename () {<br/> return true; <br/>}< br/> return false; <br/>}</P> <p> Private Static string getcomponentstring (string intentinfo) {<br/> // intent Info format: intent = # intent; action = android. intent. action. main; Category = android. intent. category. launcher; launchflags = 0x10200000; Component = com. allstar. tanzhi /. activities. startactivity; end <br/> int start = intentinfo. indexof ("component") + 9 + 1; <br/> int end = intentinfo. indexof (";", start); <br/> return intentinfo. substring (START, end); <br/>}

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.