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, 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;
}