Sometimes you need to add shortcuts on the desktop for ease of use. The following are two ways to add shortcuts:
1.
Code
Void Setshortcut (){
Intent addshortcut = New Intent ();
// Set the shortcut name
Addshortcut. putextra (intent. extra_shortcut_name, " Quick exercise " );
// Create a shortcut icon
Parcelable icon = Intent. Reset cuticonresource. fromcontext (
Shortcuttest. This , R. drawable. Icon );
// Add shortcut icon
Addshortcut. putextra (intent. extra_shortcut_icon_resource, icon );
// Build intent for quick execution
Intent mailto = New Intent ( This , Shortcuttest. Class );
// Add a shortcut intent
Addshortcut. putextra (intent. extra_shortcut_intent, mailto );
// Normal
Setresult (result_ OK, addmediacut );
}
Pass the aboveCodeYou can onlyProgramAdd in startup. You cannot add it to events. You have no idea what it is, and you have not studied it carefully. Please let me know! In addition, it is not directly added to the desktop, but to the Add shortcut window,
2.
Code
Private Void Addshortcut (){
Intent shortcut = New Intent ( " Com. Android. launcher. Action. install_shortcut " );
// Shortcut name
Shortcut. putextra (intent. extra_shortcut_name, getstring (R. String. app_name ));
// Shortcut. putextra ("DUPLICATE", false ); // Repeated creation is not allowed
// Specifies the object that the current activity is started as a shortcut, for example, Com. Everest. Video. videoplayer.
// Note: The second parameter of componentname must contain the dot (.). Otherwise, the corresponding program cannot be started in the shortcut mode.
// Componentname comp = new componentname (this. getpackagename (), "." + this. getlocalclassname ());
// Shortcut. putextra (intent. extra_shortcut_intent, new intent (intent. action_main). setcomponent (COMP ));
Shortcut. putextra (intent. extra_shortcut_intent, New Intent ( This , Shortcuttest. Class ));
// Shortcut icon
Shortcuticonresource iconres = Intent. Reset cuticonresource. fromcontext ( This , R. drawable. Icon );
Shortcut. putextra (intent. extra_shortcut_icon_resource, iconres );
Sendbroadcast (shortcut );
}
Repeated cut. putextra ("DUPLICATE", false); // indicates that repeated settings are not allowed.
This code is directly added to the desktop and displayed in the shown area. However, you need to add the following permissions:
<Uses-Permission Android: Name = "com. Android. launcher. Permission. install_shortcut"/>