A search on the Internet will create shortcuts for your own applications, but this article focuses on creating Desktop shortcuts for "specified applications.
Common Desktop shortcuts have two elements: 1. Application name 2. Application icon.
The information of the specified application icon is:
// PkgContext is the context of the specified application, and iconIdentifier is an integer that specifies the icon identifier of the application.
Export cuticonresource iconRes = Intent. Export cuticonresource. fromContext (pkgContext,
IconIdentifier );
Shortcut. putExtra (Intent. EXTRA_SHORTCUT_ICON_RESOURCE, iconRes );
The key to creating a third-party application is how to obtain the context of the third-party application. The key code is:
Context pkgContext = context. createPackageContext (pkg, Context. CONTEXT_IGNORE_SECURITY
| Context. CONTEXT_INCLUDE_CODE );
OK. The basic knowledge is finished. The following code is used directly to notify readers. /**
* @ Param context
* Performer.
* @ Params pkg name of the application package to be added with a shortcut. The value cannot be null.
* @ Return returns true to indicate that the execution is normal. <br/>
* If false is returned, the pkg value is null, or the application cannot be found, or the application has no MainActivity for Launch.
* @ Author sodino
**/
Public boolean addShortcut (Context context, String pkg ){
// Shortcut name
String title = "unknown ";
// Complete MainActivity name
String mainAct = null;
// Application icon ID
Int iconIdentifier = 0;
// Search for MainActivity by package name
PackageManager pkgMag = context. getPackageManager ();
Intent queryIntent = new Intent (Intent. ACTION_MAIN, null );
QueryIntent. addCategory (Intent. CATEGORY_LAUNCHER );
List <ResolveInfo> list = pkgMag. queryIntentActivities (queryIntent,
PackageManager. GET_ACTIVITIES );
For (int I = 0; I <list. size (); I ++ ){
ResolveInfo info = list. get (I );
If (info. activityInfo. packageName. equals (pkg )){
Title = info. loadLabel (pkgMag). toString ();
MainAct = info. activityInfo. name;
IconIdentifier = info. activityInfo. applicationInfo. icon;
Break;
}
}
If (mainAct = null ){
// No startup class
Return false;
}
Intent shortcut = new Intent ("com. android. launcher. action. INSTALL_SHORTCUT ");
// Shortcut name
Shortcut. putExtra (Intent. EXTRA_SHORTCUT_NAME, title );
// Repeated creation is not allowed
// Shortcut. putExtra ("duplicate", false );
ComponentName comp = new ComponentName (pkg, mainAct );
Shortcut. putExtra (Intent. EXTRA_SHORTCUT_INTENT,
New Intent (Intent. ACTION_MAIN). setComponent (comp ));
// Shortcut icon
Context pkgContext = null;
If (context. getPackageName (). equals (pkg )){
PkgContext = context;
} Else {
// Create a context for a third-party application to find the icon file based on the icon identifier of the application.
Try {
PkgContext = context. createPackageContext (pkg, Context. CONTEXT_IGNORE_SECURITY
| Context. CONTEXT_INCLUDE_CODE );
} Catch (NameNotFoundException e ){
E. printStackTrace ();
}
}
If (pkgContext! = Null ){
Export cuticonresource iconRes = Intent. Export cuticonresource. fromContext (pkgContext,
IconIdentifier );
Shortcut. putExtra (Intent. EXTRA_SHORTCUT_ICON_RESOURCE, iconRes );
}
// Send a broadcast to allow the receiver to create a shortcut
// Permission <uses-permission
// Android: name = "com. android. launcher. permission. INSTALL_SHORTCUT"/>
Context. sendBroadcast (shortcut );
Return true;
}