Create Desktop shortcuts for mobile apps, and create shortcuts for apps
Preview:
Permission required:
1 <uses-permission android: name = "com. android. launcher. permission. INSTALL_SHORTCUT"/>
Configuration File: AndroidManifest. xml
1 <activity2 android:name="com.myself.news.activity.GuideActivity"3 android:label="@string/title_activity_guide" >4 <intent-filter>5 <action android:name="com.myself.news.ACTION_HOME" />6 7 <category android:name="android.intent.category.DEFAULT" />8 </intent-filter>9 </activity>
The oncreate method of the Activity on the application's pop-up page calls installShortcut ();
Code:
1 // create a shortcut 2 // com. android. launcher. permission. INSTALL_SHORTCUT 3 private void installShortcut () {4 // determine whether a shortcut has been created 5 boolean isCreated = SharedPreferencesUtils. getBoolean (this, 6 GlobalConstantsUtils. PREF_IS_SHORTCUT_INTALLED, false); 7 // determine whether 8 if (! IsCreated) {9 // broadcast 10 Intent intent = new Intent (); 11 intent. setAction ("com. android. launcher. action. INSTALL_SHORTCUT "); 12 13 // icon 14 // generate a Bitmap object 15 intent based on the resource file id. putExtra (Intent. EXTRA_SHORTCUT_ICON, BitmapFactory16. decodeResource (getResources (), R. drawable. ic_launcher); 17 // name 18 intent. putExtra (Intent. EXTRA_SHORTCUT_NAME, "mobile security guard"); 19 20 // Action 21 Intent actionIntent = new Intent (); 22 // jump to page 23 actionIntent. setAction (GlobalConstantsUtils. ACTION_HOME); 24 25 intent. putExtra (Intent. EXTRA_SHORTCUT_INTENT, actionIntent); 26 sendBroadcast (intent); 27 28 // mark the created shortcuts. 29 SharedPreferencesUtils will not be created next time. setBoolean (this, 30 GlobalConstantsUtils. PREF_IS_SHORTCUT_INTALLED, true); 31} 32}
Constant tool class GlobalConstantsUtils:
1 public static final String PREF_IS_SHORTCUT_INTALLED = "is_shortcut_intalled"; // whether a shortcut has been created
1 public static final String ACTION_HOME = "com. myself. news. ACTION_HOME"; // jump to the ACTION on the home page