[Android] creates a desktop shortcut for a specified application

Source: Internet
Author: User

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

 

 

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.