Android-create shortcuts and determine whether a shortcut has been created

Source: Internet
Author: User

Generally, shortcuts are not automatically created after the android application is installed. Therefore, you can implement shortcuts when the application is started.

Permission required <uses-Permission Android: Name = "com. Android. launcher. Permission. install_shortcut"/>

The attribute of the activity corresponding to the shortcut to be clicked.

<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
<Intent-filter>
<Action Android: Name = "android. Intent. Action. create_shortcut"> </Action>
</Intent-filter>

How can I determine whether a shortcut has been created? Because the shortcut information is saved in the favorites table of launcher. dB of COM. Android. launcher, You can query this table to obtain it,

Permission required <uses-Permission Android: Name = "com. Android. launcher. Permission. read_settings"/>

The following is an example:

</P> <p> Import android. app. activity; <br/> Import android. content. contentresolver; <br/> Import android. content. intent; <br/> Import android. content. intent. export cuticonresource; <br/> Import android. database. cursor; <br/> Import android. graphics. bitmapfactory; <br/> Import android.net. uri; <br/> Import android. OS. bundle; <br/> public class addshortcutactivity extends activity {<br/>/** called when the act Ivity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> Boolean flag = ifaddshortcut (); // if it has been created, you do not need to create it in <br/> If (flag = false) {<br/> addshortcut (); <br/>}< br/> Public void addshortcut () {<br/> intent shortcut = new intent ("com. android. launcher. action. install_shortcut "); <Br/> // set properties <br/> cut. putextra (intent. extra_shortcut_name, getresources (). getstring (R. string. app_name); <br/> export cuticonresource iconres = intent. using cuticonresource. fromcontext (addshortcutactivity. this, R. drawable. icon); <br/> shortcut cut. putextra (intent. extra_shortcut_icon, iconres); </P> <p> // whether repeated creation is allowed <br/> cancel cut. putextra ("DUPLICATE", false); <br/> intent = new intent (intent. actio N_main); <br/> intent. setflags (intent. flag_activity_reset_task_if_needed); <br/> intent. addflags (intent. flag_activity_launched_from_history); <br/> intent. addcategory (intent. category_launcher); <br/> intent. setclass (addshortcutactivity. this, addshortcutactivity. class); <br/> // set the Startup Program <br/> system. out. println ("createicon"); <br/> shortcut cut. putextra (intent. extra_shortcut_intent, intent); <br/> add1_cu Tactivity. this. sendbroadcast (shortcut); <br/>}< br/> Public Boolean ifaddshortcut () {<br/> Boolean isinstallshortcut = false; <br/> final contentresolver Cr = addshortcutactivity. this. getcontentresolver (); <br/> // your 2.2 system is "com. android. launcher2.settings ". For more information, see" com. android. launcher. settings "<br/> final string authority =" com. android. launcher2.settings "; <br/> final URI content_uri = Uri. parse ("Content: //" + <br/> authority + "/favorites? Required y = true "); <br/> cursor c = CR. query (content_uri, <br/> New String [] {"title", "iconresource" },< br/> "Title =? ", <Br/> New String [] {getstring (R. String. app_name)}, null); // XXX indicates the application name. <Br/> If (C! = NULL & C. getcount ()> 0) {<br/> isinstallshortcut = true; <br/> system. out. println ("created"); <br/>}< br/> return isinstallshortcut; <br/>}< br/>}


<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. shao. add "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <uses-SDK Android: minsdkversion = "8"/> </P> <p> <application Android: icon = "@ drawable/icon" Android: label = "@ string/app_name"> <br/> <activity Android: Name = ". addshortcutactivity "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> <action Android: Name =" android. intent. action. create_shortcut "> </Action> <br/> </intent-filter> <br/> </activity> </P> <p> </Application> <br/> <uses-Permission Android: name = "com. android. launcher. permission. read_settings "/> <br/> <uses-Permission Android: Name =" com. android. launcher. permission. install_shortcut "/> <br/> </manifest>



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.