Example of creating and deleting shortcuts in Android

Source: Internet
Author: User

In Android, examples of shortcut creation and deletion are supported in the Android system. This allows users to use applications or software faster and better. Of course, some systems provide the function of creating shortcuts by drag and drop. Here we will briefly introduce how to create and delete Shortcuts Using code in android. The following source code is from the internet. I will make some modifications and comments. The principle is mainly to use a broadcast mechanism of the android system. What we need to do is to instance an Intent object, set the object, and then send a broadcast, then other operations can be delegated to other programs, which is so simple. The source code is as follows.

1. package ps. androidyue. shortcutdemo;
2.
3. import android. app. Activity;
4. import android. content. ComponentName;
5. import android. content. Intent;
6. import android. content. Intent. Export cuticonresource;
7. import android. OS. Bundle;
8. import android. view. View;
9. import android. widget. Button;
10.
11. public class ShortCutDemoActivity extends Activity {
12. // constant action for installing and uninstalling shortcuts
13. private final String INTENT_ACTION_INSTALL_SHORTCUT = "com. android. launcher. action. INSTALL_SHORTCUT ";
14. private final String INTENT_ACTION_UNINSTALL_SHORTCUT = "com. android. launcher. action. UNINSTALL_SHORTCUT ";
15. private Button btnAddShortCut, btnRemoveShortCuts;
16./** Called when the activity is first created .*/
17. @ Override
18. public void onCreate (Bundle savedInstanceState ){
19. super. onCreate (savedInstanceState );
20. setContentView (R. layout. main );
21. this. initializeViews ();
22 .}
23.
24 ./*
25. * initialize the required View
26 .*/
27. private void initializeViews (){
28. this. initializeButtons ();
29 .}
30.
31 ./*
32. * initialize the required button
33 .*/
34. private void initializeButtons (){
35. this. btnAddShortCut = (Button) this. findViewById (R. id. btnAddShortCuts );
36. this. btnAddShortCut. setOnClickListener (new View. OnClickListener (){
37.
38. @ Override
39. public void onClick (View v ){
40. // Add shortcuts
41. addShortcut ();
42 .}
43 .});
44. this. btnRemoveShortCuts = (Button) this. findViewById (R. id. btnRemoveShortCuts );
45. this. btnRemoveShortCuts. setOnClickListener (new View. OnClickListener (){
46.
47. @ Override
48. public void onClick (View v ){
49. // Delete shortcuts
50. delemedicut ();
51 .}
52 .});
53 .}
54.
55.
56 ./*
57. * Create a desktop shortcut for the program
58 .*/
59. private void addShortcut (){
60. // instantiate the Intent object with installation shortcuts
61. Intent shortcut = new Intent (this. INTENT_ACTION_INSTALL_SHORTCUT );
62. // shortcut name
63. shortcut. putExtra (Intent. EXTRA_SHORTCUT_NAME, getString (R. string. app_name ));
64. Repeated cut. putExtra ("duplicate", false); // repeated creation is not allowed.
65.
66. // specify the current Activity as the object for Quick Start, such as com. everest. video. VideoPlayer.
67. // Note: The second parameter of ComponentName must contain the dot (.). Otherwise, the corresponding program cannot be started in the shortcut.
68. ComponentName comp = new ComponentName (this. getPackageName (), "." + this. getLocalClassName ());
69. shortcut. putExtra (Intent. EXTRA_SHORTCUT_INTENT, new Intent (Intent. ACTION_MAIN). setComponent (comp ));
70. // shortcut icon
71. Reset cuticonresource iconRes = Intent. Reset cuticonresource. fromContext (this, R. drawable. ic_launcher );
72. shortcut. putExtra (Intent. EXTRA_SHORTCUT_ICON_RESOURCE, iconRes );
73. // send the broadcast that creates a shortcut
74. sendBroadcast (shortcut );
75 .}
76.
77 ./*
78. * shortcut for deleting a program
79 .*/
80. private void delShortcut (){
81. Intent shortcut = new Intent (this. INTENT_ACTION_UNINSTALL_SHORTCUT );
82.
83. // The shortcut name www.2cto.com
84. shortcut. putExtra (Intent. EXTRA_SHORTCUT_NAME, getString (R. string. app_name ));
85.
86. // specify the current Activity as the object for quick start, for example, com. everest. video. VideoPlayer.
87. // Note: The second parameter of ComponentName must be a complete Class Name (package name + class name); otherwise, the shortcut cannot be deleted.
88. String appClass = this. getPackageName () + "." + this. getLocalClassName ();
89. ComponentName comp = new ComponentName (this. getPackageName (), appClass );
90. shortcut. putExtra (Intent. EXTRA_SHORTCUT_INTENT, new Intent (Intent. ACTION_MAIN). setComponent (comp ));
91. // send and uninstall the shortcut icon
92. sendBroadcast (shortcut );
93.
94 .}
95 .}
 
Note: You must declare the permission in manifest. xml.

1. <uses-permission android: name = "com. android. launcher. permission. INSTALL_SHORTCUT"/>
2. <uses-permission android: name = "com. android. launcher. permission. UNINSTALL_SHORTCUT"/>
 

From BossDarcy's column

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.