Create and delete Desktop shortcuts for Android

Source: Internet
Author: User

Some users reported that after creating a shortcut, they cannot start it after clicking the shortcut.ProgramOr prompt "not installed", it seems that the New rom has been modified in the shortcut section (because this article was issued in May, it is estimated that it should be 2.0 or 2.1 ROM ), corrected. HTC G11 2.3.5rom passed the test.

1. Determine whether shortcuts have been created (in some models, you need to determine)

 
Private Boolean hasshortcut () {Boolean isinstallshortcut = false; Final contentresolver Cr = activity. getcontentresolver (); final string authority = "com. android. launcher. settings "; Final URI content_uri = Uri. parse ("content: //" + authority + "/favorites? Required y = true "); cursor c = Cr. Query (content_uri, new string [] {" title "," iconresource "}," Title =? ", New string [] {mapviewactivity. getstring (R. String. app_name). Trim ()}, null); If (C! = NULL & C. getcount ()> 0) {isinstallshortcut = true;} return isinstallshortcut ;}

2. Create

 
/*** Create a desktop shortcut for the Program */private void addshortcut () {intent shortcut = new intent ("com. android. launcher. action. install_shortcut cut "); // shortcut name: shortcut cut. putextra (intent. extra_shortcut_name, getstring (R. string. app_name); shortcut cut. putextra ("DUPLICATE", false ); // duplicate creation is not allowed * * ********************* // componentname comp = new componentname (this. getpackagename (),". "+ this. getlocalclassname (); // shortcut cut. putextra (intent. extra_shortcut_intent, new intent (intent. action_main ). setcomponent (COMP )); /**************************** end ******** * *********************/intent shortcutintent = new intent (intent. action_main); shortcutintent. setclassname (this, this. getclass (). getname (); shortcut cut. putextra (intent. extra_shortcut_intent, shortcutintent); // shortcut icon export cuticonresource iconres = intent. using cuticonresource. fromcontext (this, R. drawable. icon); shortcut cut. putextra (intent. extra_shortcut_icon_resource, iconres); sendbroadcast (shortcut );}

3. Delete

 

 
/*** Shortcut for deleting a program */private void delshortcut () {intent shortcut = new intent ("com. android. launcher. action. uninstall_shortcut cut "); // shortcut name: shortcut cut. putextra (intent. extra_shortcut_name, getstring (R. string. app_name); string appclass = This. getpackagename () + ". "+ this. getlocalclassname (); componentname comp = new componentname (this. getpackagename (), appclass); shortcut. putextra (intent. extra_shortcut_intent, new intent (intent. action_main ). setcomponent (COMP); sendbroadcast (shortcut );}

3. Declare Permissions

Declare permissions when creating and deleting shortcuts in the androidmanifest. xml file

 
   
   
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.