Android shortcut cut operation (shortcut)

Source: Internet
Author: User

1Add to the shortcut cut option

/*** Add to the shortcut option (by default, the desktop is long and pressed for calling out) ** at the same time, you need to provide a function for the activity in manifest that contains * Action = "android. intent. action. create_shortcut "intent-filter */private void addshortcuttooptions () {// create a default intent shortcut = new intent (); // The shortcut name shortcut. putextra (intent. extra_shortcut_name, getstring (R. string. app_name); // duplicate cut cannot be created. putextra ("DUPLICATE", false); // specify the current activity as the object to be started in the shortcut mode: such as COM. everest. video. videoplayer // Note: The second parameter of componentname must contain the dot (.), otherwise, the shortcut cannot start the corresponding Program String appclass = This. getpackagename () + ". "+ this. getlocalclassname (); componentname comp = new componentname (this. getpackagename (), appclass); shortcut. putextra (intent. extra_shortcut_intent, newintent (intent. action_main ). setcomponent (COMP); // The method below is the same as the above, and the other construction form is only // intent respondintent = new intent (this, this. getclass (); // extract cut. putextra (intent. extra_shortcut_intent, respondintent); // shortcut icon export cuticonresource iconres = intent. using cuticonresource. fromcontext (this, R. drawable. icon); shortcut cut. putextra (intent. extra_shortcut_icon_resource, iconres); // setresult (result_ OK, shortcut) sent to the Message Queue );}

2.Create Desktop shortcuts for Programs

/*** Create a desktop shortcut for the program ** and set the following permissions in manifest: * <uses-Permission Android: Name = "com. android. launcher. permission. install_shortcut "/> */private void addshortcut () {intent shortcut = newintent (" com. android. launcher. action. install_shortcut cut "); // shortcut name: shortcut cut. putextra (intent. extra_shortcut_name, getstring (R. string. app_name); // duplicate cut cannot be created. putextra ("DUPLICATE", false); // specify the current activity as the object to be started in the shortcut mode: such as COM. everest. video. videoplayer // here, an action must be set for intent, which can be arbitrary (but this parameter must be consistent during installation and uninstallation) string action = "com. android. action. test "; intent respondintent = new intent (this, this. getclass (); respondintent. setaction (action); cut. putextra (intent. extra_shortcut_intent, respondintent); // The following method has the same effect as the above method, and another construction method only. // Note: The second parameter of componentname must contain the dot (.), otherwise, the shortcut cannot start the corresponding program // string appclass = This. getpackagename () + ". "+ this. getlocalclassname (); // componentname comp = new componentname (this. getpackagename (), appclass); // shortcut cut. putextra (intent. extra_shortcut_intent, new intent (action ). setcomponent (COMP); // shortcut icon ‑ cuticonresource iconres = intent. using cuticonresource. fromcontext (this, R. drawable. icon); shortcut cut. putextra (intent. extra_shortcut_icon_resource, iconres); sendbroadcast (shortcut );}

3.Shortcut for deleting a program

/* ** Delete the shortcut of the program ** at the same time, you need to set the following permissions in manifest: * <uses-Permission Android: Name = "com. android. launcher. permission. uninstall_shortcut "/> */private void delemedicut () {intent shortcut = newintent (" com. android. launcher. action. uninstall_shortcut cut "); // shortcut name: shortcut cut. putextra (intent. extra_shortcut_name, getstring (R. string. app_name); // specify the current activity as the object for Quick Start: such as COM. everest. video. videoplayer // here, an action must be set for intent, which can be arbitrary (but this parameter must be consistent during installation and uninstallation) string action = "com. android. action. test "; intent respondintent = new intent (this, this. getclass (); respondintent. setaction (action); cut. putextra (intent. extra_shortcut_intent, respondintent); // The following method has the same effect as the above method, and another construction method only. // Note: The second parameter of componentname must contain the dot (.), otherwise, the shortcut cannot start the corresponding program // string appclass = This. getpackagename () + ". "+ this. getlocalclassname (); // componentname comp = new componentname (this. getpackagename (), appclass); // shortcut cut. putextra (intent. extra_shortcut_intent, new intent (action ). setcomponent (COMP); sendbroadcast (shortcut );}

From: http://www.eoeandroid.com/thread-92125-1-1.html

4. determine whether there is a shortcut

/*** Determine whether a shortcut has been added to the desktop ** @ Param CX * @ Param titlename * shortcut name * @ return */public static Boolean hasshortcut (context CX) {boolean result = false; // obtain the name of the current application String title = NULL; try {final packagemanager PM = Cx. getpackagemanager (); Title = PM. getapplicationlabel (PM. getapplicationinfo (Cx. getpackagename (), packagemanager. get_meta_data )). tostring ();} catch (exception e) {} final string uristr; I F (Android. OS. Build. version. sdk_int <8) {uristr = "content: // com. Android. launcher. Settings/favorites? Required y = true ";} else {uristr =" content: // com. Android. launcher2.settings/favorites? Required y = true ";} final URI content_uri = URI. parse (uristr); Final cursor c = Cx. getcontentresolver (). Query (content_uri, null," Title =? ", New string [] {Title}, null); If (C! = NULL & C. getcount ()> 0) {result = true;} return result ;}

The above content was found on the Internet, but I found that I always returned false when checking whether there is a shortcut on Samsung 4.0. Then I looked at the system source code and found a way for the system to determine whether there is a shortcut.

 
/*** Returns true if the shortcuts already exists in the database. * We identify a shortcut by its title and intent. */static Boolean initialize cutexists (context, String title, intent) {final contentresolver Cr = context. getcontentresolver (); cursor c = CR. query (launchersettings. favorites. content_uri, new string [] {"title", "intent"}, "Title =? And intent =? ", New string [] {Title, intent. touri (0)}, null); boolean result = false; try {result = C. movetofirst ();} finally {C. close ();} return result ;}

Launchersettings. favorites. content_uri = URI. parse ("content: // com. Android. launcher2.settings/favorites? Y = true ")

The intent in the above method refers to the intent used to create a shortcut. The above method is probably used to determine whether the database has the same title, intent converts the string to the same data, and the shortcut exists.

Well, the problem still persists. If this table is queried to get null, Samsung has changed the database!

Do not trust anything on the internet. Code There is a problem, that is, the first login program, press home, and then create another activity without restoring the original, note: there is also a situation that can cause this phenomenon, that is, setting the launchmode such as singletask for the first activity. Add the following sentence to the Code in another form:
 
Respondintent. addcategory ("android. Intent. Category. launcher ");

Fix this problem.

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.