Create a desktop shortcut

Source: Internet
Author: User

Adding a desktop shortcut is very simple. You only need to perform three steps: 1. Create an Intent for adding a shortcut. The Intent Action is com. android. launcher. action. INSTALL_SHORTCUT. 2. You can add the Extra attribute to the Intent to set the title, icon, and Startup Program of the shortcut. 3. Call the sendBroadcast () method to send a broadcast to add a shortcut. The following is a simple example. In this application, only the content of the desktop shortcut is provided. The specific application of the program does not need to be provided. After the program is installed for the first time, A shortcut will be created on the desktop and will not be created later. The Code is as follows: Activity: [java] package com. home. activity; import com. example. testshortcut. r; import android. app. activity; import android. content. intent; import android. content. sharedPreferences; import android. content. sharedPreferences. editor; import android. OS. bundle; import android. OS. parcelable; import android. widget. toast; public class TestShortcutActivity extends Activity {private SharedPreferences s P; private Editor editor; private int count = 1; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // get the SharedPreferences object sp = this. getSharedPreferences ("testshortcut", MODE_PRIVATE); // obtain the Editor object editor = sp. edit (); // read the value int readCount = sp. getInt ("count", 0); if (readCount> 0) {Toast. makeText (this, "the shortcut already exists and you do not need to create it again", Toast. LENGTH_LONG ). show (); return;} // create Intent addIntent = new Intent ("com. android. launcher. action. INSTALL_SHORTCUT cut "); // the title of the shortcut String title =" My applications "; // load the shortcut icon Parcelable icon = Intent. using cuticonresource. fromContext (this, R. drawable. ic_launcher); // create a program that starts again after clicking the shortcut. Start Intent myIntent = new Intent (this, TestShortcutActivity here. clas S); // set the title of the shortcut addIntent. putExtra (Intent. EXTRA_SHORTCUT_NAME, title); // sets the shortcut icon addIntent. putExtra (Intent. EXTRA_SHORTCUT_ICON_RESOURCE, icon); // sets the Intent addIntent corresponding to the shortcut. putExtra (Intent. EXTRA_SHORTCUT_INTENT, myIntent); // send broadcast add shortcuts sendBroadcast (addIntent); // write the count to the file editor. putInt ("count", count); // submit the modify editor. commit () ;}} package com. home. activity; import com. example. testshortcut. r; Import android. app. activity; import android. content. intent; import android. content. sharedPreferences; import android. content. sharedPreferences. editor; import android. OS. bundle; import android. OS. parcelable; import android. widget. toast; public class TestShortcutActivity extends Activity {private SharedPreferences sp; private Editor editor; private int count = 1; @ Overrideprotected void onCreate (Bundle save DInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // get the SharedPreferences object sp = this. getSharedPreferences ("testshortcut", MODE_PRIVATE); // obtain the Editor object editor = sp. edit (); // read the value int readCount = sp. getInt ("count", 0); if (readCount> 0) {Toast. makeText (this, "the shortcut already exists and you do not need to create it again", Toast. LENGTH_LONG ). show (); return;} // create IntentIntent add for adding shortcuts Intent = new Intent ("com. android. launcher. action. INSTALL_SHORTCUT cut "); // the title of the shortcut String title =" My applications "; // load the shortcut icon Parcelable icon = Intent. using cuticonresource. fromContext (this, R. drawable. ic_launcher); // create a program that starts again after clicking the shortcut. Start Intent myIntent = new Intent (this, TestShortcutActivity here. class); // sets the title of the shortcut addIntent. putExtra (Intent. EXTRA_SHORTCUT_NAME, title); // sets the shortcut icon addIntent. putExtra (Intent. EXTRA_SHO RTCUT_ICON_RESOURCE, icon); // sets the IntentaddIntent corresponding to the shortcut. putExtra (Intent. EXTRA_SHORTCUT_INTENT, myIntent); // send broadcast add shortcuts sendBroadcast (addIntent); // write the count to the file editor. putInt ("count", count); // submit the modify editor. commit () ;}} permission: [html] <uses-permission android: name = "com. android. launcher. permission. INSTALL_SHORTCUT "/> <uses-permission android: name =" com. android. launcher. permission. INSTALL_SHORTCUT "/> Activity configuration: [htm L] <activity android: name = "com. home. activity. testShortcutActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> <! -- Define to add to the desktop Launcher --> <intent-filter> <action android: name = "android. intent. action. CREATE_SHORTCUT "/> </intent-filter> </activity> <activity android: name =" com. home. activity. testShortcutActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> <! -- Define to add to the desktop Launcher --> <intent-filter> <action android: name = "android. intent. action. CREATE_SHORTCUT "/> </intent-filter> </activity>

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.